I know that calling the StateHasChanged()
method notifies the component that the state has changed and that it should re-render.
However, I also see calls to await InvokeAsync(StateHasChanged)
or await InvokeAsync(() => StateHasChanged())
in other people's code, but I don't quite understand how it compares to StateHasChanged()
and when should one be favored over the other, and why.
The only information I could find was this part of the Blazor docs, it says:
In the event a component must be updated based on an external event, such as a timer or other notifications, use the InvokeAsync method, which dispatches to Blazor's synchronization context.
I don't quite get this. It just says "...which dispatches to Blazor's synchronization context", but I'm not quite satisfied with that! What is "Blazor's synchronization context"?
I have tried calling StateHasChanged()
- instead of InvokeAsync(StateHasChanged)
- in a Timer
's Elapsed
event, and it works as expected, without any issues. Should I be calling await InvokeAsync(StateHasChanged)
instead?! And if so, why exactly? I feel like there's probably some important nuance here that I'm unaware of.
I've also seen calls like InvokeAsync(() => InvokeAsync(Something))
, again, why?
Plus, I also sometimes see InvokeAsync()
called without await
, what the deal with that?!