Anthony Ma
Anthony Ma

Reputation: 686

Retrieving value within RenderFragment

I would like to retrieve the value of a RenderFragment for use in my code:

Parent Component

<SkillIndividual>
  <Skill>Fireball</Skill>
  <AttributeType>Fire</AttributeType>
</SkillIndividual>

Child Component (SkillIndividual.razor)

<span>@Skill</span> <span class="semi-transparent-text">(@AttributeType)</span>

@code {
  [Parameter] public RenderFragment Skill { get; set; }
  [Parameter] public RenderFragment AttributeType { get; set; }

  private void CheckValue()
  {
    // Check the value of Skill (Fireball in this case)
  }
}

I know I can create [Parameter] public string Skill {get;set;} but I feel like that would ruin the flow of my code. If I can take the value from RenderFragment that would make the code a lot cleaner.

Upvotes: 3

Views: 3583

Answers (1)

enet
enet

Reputation: 45666

A RenderFragment is a delegate type and you cannot access its constituent parts. Ordinarily, when you want to relate to a child content of a component, you define a single property of type RenderFragment which is named by convention ChildContent.

In short, you cannot extract values from these delagtes. They are to be executed and rendered only. What's the issue with Component parameters ?

Upvotes: 3

Related Questions