Search code examples
razorblazorblazor-webassembly

Blazor component referencing attribute value directly from another attribute


I have an external blazor component with 2 attributes like below. I am resolving through a function by passing the value of Href attribute.

 <ComponentLink  Href="bookings" Active=@GetCurrentLink("bookings")>

1my question is there any way to reference or bind the value of attribute value directly from another attribute without passing through a variable.

Basically something like

<ComponentLink  Href="bookings" Active=@GetCurrentLink(@Href)>

I am asking this because it is within link list for a navigation and i dont want to create a fiel or property for each Href element. I can fix it on the ComponentLink source code possibly but I don't have source code access as it is in nuget package. I have tried using @ref but It also doesnt help as intented.


Solution

  • There is no easy way to achieve this.

    You can look at the component definition in the same way as the class constructors. You will have the same problem when you want to pass the same value there.

    The best option, in this case, is to extract the text in the variable and reuse it. You can also define your links in your C# code as an array and just enumerate the links to render the ComponentLink components dynamically. Here is an example of this approach: https://blazorrepl.telerik.com/wmFYwAvG39LqBZFb06

    The option with the @ref is not working, because the @ref value is populated once the component is rendered and typically, you will need to evaluate the Active value before that.