question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Blazor] Explicitly set values should trump splatted attributes

See original GitHub issue

The position of @attributes relative to the position of element attributes is important. When @attributes are splatted on the element, the attributes are processed from right to left (last to first)

https://docs.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-3.1

Attributes having to have a special order is not immediately obvious when trying to understand why an explicitly set parameter value is incorrect.

The position of @attributes should be irrelevant. The @attributes should be splatted first so that explicitly set parameters cannot be overridden by the consumer of the component.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mrpmorriscommented, Jul 28, 2020

Ah no, not that it matters now, but you were thinking of a different approach.

All you have to do is this

[Parameter]
public string Something { get; set; }

protected override async SetParametersAsync(ParameterView parameters)
{
  await base.SetParametersAsync(parameters);
  if (Something is null)
    Something = "the default value";
}
0reactions
javiercncommented, Jul 28, 2020

I don’t understand what this means.

The way SetParametersAsync is implemented today, if you override you’ll have to call base for things like OnInitialized to run when extending componentbase (the most common case). If you override, do something and call base afterwards, it will have no effect since the base implementation will set the parameters. If you call base and then do something afterward, all the OnInititalized and OnParametersSet will run with the wrong set of parameter values. That is something we could fix by introducing an additional lifecycle method that can be overriden, but we haven’t seen the need for it yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core Blazor attribute splatting and arbitrary ...
Learn how components can capture and render additional attributes in addition to the component's declared parameters.
Read more >
Blazor Attribute Splatting
In this article we will discuss, what is Attribute Splatting in Blazor with ... It is tedious to declare these parameters in code...
Read more >
Capturing unmatched attributes and attribute splatting in ...
In this post, I describe how to use attribute splatting in ASP. ... @attributes will be expanded with all unmatched parameters *@ <input ......
Read more >
Blazor Attribute Splatting Fails with Int Values · Issue #22048
If I specify MaxLength on RadzenTextBox within the component, it works. If I specify MaxLength when declaring the component, however, it fails.
Read more >
Is there anyway to get attribute splatting to work with ...
There is a hacky work around I found but it leaves much to be desired. Does anyone know how I could possible get...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found