What is the purpose of StateHasChanged(), and when does the framework call it automatically vs when must you call it manually?
Last updated
StateHasChangedmarks the component dirty and schedules a render, which builds a new render tree and applies only the diff.The framework calls it automatically after UI event handlers, lifecycle methods (including async continuations), and parent parameter changes.
Call it manually for changes from timers, background tasks, or external events. From another thread use
await InvokeAsync(StateHasChanged).
- https://learn.microsoft.com/en-us/aspnet/core/blazor/components/rendering?view=aspnetcore-10.0
- https://learn.microsoft.com/en-us/aspnet/core/blazor/performance/rendering?view=aspnetcore-10.0
- https://medium.com/@syeedmdtalha/shouldrender-and-statehaschanged-in-blazor-mastering-ui-rendering-control-9dd4509eed20
- https://github.com/dotnet/aspnetcore/discussions/29318
- https://www.syncfusion.com/blogs/post/master-state-management-in-blazor
- https://blazor-university.com/components/multi-threaded-rendering/invokeasync/
- https://learn.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle?view=aspnetcore-10.0
- https://www.telerik.com/blogs/blazor-basics-blazor-component-re-rendering