Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Blazor Framework get notifed when the property value gets changed

When we have a HTML code like below.

<h1>@Title</h1>

and C# like below

public string Title { get; set; }

Think I have executed a code like below

Title = "New title updated in the runtime";

Then we see that the view has chenged with the new value.

How the renderer knows that the property value has changes. Is there anything like INotifyProperyChange is implemented. How the HTML knows that this is the time to update the content.

I just want to know how the internals of the Blazor Framework are architectured.

like image 558
VibeeshanRC Avatar asked Feb 22 '18 05:02

VibeeshanRC


People also ask

What does @bind do Blazor?

To use two-way binding on a parameter simply prefix the HTML attribute with the text @bind- . This tells Blazor it should not only push changes to the component, but should also observe the component for any changes and update its own state accordingly.

Is anyone using Blazor in production?

Yes, Blazor is ready to be used in production, it is a life changer, Blazor is a framework that optimizes development speed, helps considerably reducing code duplication and inconsistencies between front-end and api/backend models, and in turn it helps reducing bugs and development costs, in top of that, since Blazor ...

Is Blazor a future?

Blazor, which conveniently is a short form of browser + razor, is a relatively new Microsoft web framework that was released in 2018. Similar to Angular, Blazor is both open source and free. The framework enables developers to build interactive and reusable web UI in existing web technologies like C# or HTML.

What does StateHasChanged do Blazor?

StateHasChanged notifies the component that its state has changed. When applicable, calling StateHasChanged causes the component to be rerendered. StateHasChanged is called automatically for EventCallback methods. For more information on event callbacks, see ASP.NET Core Blazor event handling.


1 Answers

Blazor automatically checks for changes of bound properties when a user interaction happened (e.g. button click, text in an input changed). In that case, Blazor's JavaScript code triggers change detection in C# (=Webassembly). Therefore, you do not need to do anything if you want to refresh the UI after a user interaction.

However, there are cases in which you want to refresh the UI although no user interaction happened (e.g. timer). In that case, you have to call StateHasChanged. I have created a more detailed example on learn-blazor.com.

like image 95
Rainer Avatar answered Oct 03 '22 23:10

Rainer