StateHasChanged() needs to be called on every component
See original GitHub issueI setup a handler like so in the main top level component.
@functions
{
protected override void OnInit()
{
state.OnChange += StateHasChanged;
}
}
However I had to add this code to every component that cared about state, which is most components. I would have thought being at the top level that it would have rerendered and diffed the whole tree but it only seems to do component by component (which is obviously great for performance).
In which case, the component should really subscribe to state it is interested in.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
StateHasChanged() needs to be called on every component
I setup a handler like so in the main top level component. @functions { protected override void OnInit() { state.OnChange += StateHasChanged ......
Read more >When should I call StateHasChanged and when Blazor ...
A call to StateHasChanged is received by ComponentBase (standard base class for any Blazor component). This is the one that contains the logic ......
Read more >Is calling StateHasChanged() on a small component ...
Whenever the progressbar of one item is updated, I want to display it on the UI, thus have to call StateHasChanged() . Is...
Read more >State Hasn't Changed? Why and when Blazor components ...
Sometimes a quick call to StateHasChanged is all you need to get back up and running but what's really going on behind the...
Read more >ASP.NET Core Razor component lifecycle
State changes ( StateHasChanged ). StateHasChanged notifies the component that its state has changed. When applicable, calling StateHasChanged ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Exactly - that is the expected design. It makes sense for your state class to expose different events corresponding to different state changes, and then your presentation components can subscribe to the ones they care about. This gives you whatever update granularity you require.
I hope that in the near future there will be .NET state container frameworks that make this simpler to achieve, including some kind of hierarchy of state where you could subscribe at a given level and receive notifications when changes are posted at any child level.
Right now if you really want to have a single state change event, and have all your components re-render when it changes, you could create a custom base class for all your components whose
Init
method adds the event handler. Remember to implementIDisposable
and unregister the event handler on disposal, otherwise you’ll be leaking memory as the set of components changes over time.@FrikkinLazer Yeah, server-side Blazor is still a work in progress. One of our major focuses right now is getting server-side Blazor ready for production usage. The team is shifting focus from feature work to fundamentals like stress, perf, security, etc.
The
StateHasChanged()
method is the signal used to indicate that some state the component’s rendering logic depends on has changed and the component should be rerendered. Typically the component’s state is completely under it’s own control. If you need to pass state into a component you do that with component parameters. if the component subscribes to some event that it uses to then update its state, then that’s a common case where you need to callStateHasChanged()
manually.We have quite a few folks building reusable components for Blazor today (e.g. Telerik, DevExpress, Syncfusion. Some of these components are pretty elaborate.
I’d be interested to know what are the cases where you are finding this model unusable. Please open an issue on the https://github.com/aspnet/aspnetcore repo describing the component you’re trying to implement and we’ll see if we can help you out.