Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

blazor - Two-way binding and background update does not re-render component

I have a Blazor server-side project, where I have a custom component that uses a viewmodel to bind to inputbase checkbox.

If I change the viewmodel by clicking on a button, everything works fine. But if I change the viewmodel by something more "background", like a GRPC push message, then the inputbase checkbox is not re-rendered.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As long as the reason for a change is something related to UI, like a click of a button, an input change in a field, etc., in most cases, the render cycle is kicked off by Blazor.

However, if the reason is something more nested like a timer or a finished asynchronous call, you can use the method StateHasChanged in a component to trigger a new render cycle.

As mentioned in the comment, there might be drawbacks when this method is called outside the UI thread. await InvokeAsync(StateHasChanged) helps to mitigate the issue. Besides, it is async like nearly which is more Blazor "serverish".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...