Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 319
Anthony Ma Avatar asked Nov 01 '25 18:11

Anthony Ma


1 Answers

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 ?

like image 127
enet Avatar answered Nov 04 '25 14:11

enet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!