Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

StateHasChanged() vs InvokeAsync(StateHasChanged) in Blazor

$
0
0

I know that calling the StateHasChanged() method notifies the component that the state has changed and therefore it should re-render.

However, I also sometimes see things like await InvokeAsync(StateHasChanged) or await InvokeAsync(() => StateHasChanged()) in other people's code, but I'm not sure how they're different from StateHasChanged() and where one of them should be preferred over the others, and why.

The only information I could find was this part of the Blazor docs, stating that:

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 understand this. It just says "...which dispatches to Blazor's synchronization context", I'm not satisfied with that, what exactly 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?!

Too many questions sorry! :D


Viewing all articles
Browse latest Browse all 4839

Trending Articles