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.

StateHasChanged() needs to be called on every component

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
SteveSandersonMScommented, Mar 29, 2018

In which case, the component should really subscribe to state it is interested in.

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 implement IDisposable and unregister the event handler on disposal, otherwise you’ll be leaking memory as the set of components changes over time.

0reactions
danroth27commented, Jun 12, 2019

@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 call StateHasChanged() 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.

Read more comments on GitHub >

github_iconTop 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 >

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