You have a main component and inside the main component you have many sub-components
You want to refresh a single subcomponent, rather than the entire screen of the main component, is this possible?
To force a component to rerender, use the “StateHasChanged” method in Blazor, to notify that the state has been changed and requires re-rendering. For more information on the same, check this link.
Blazor detects the UI changes in common scenarios like EventCallback (button click, dropdown select, etc.), and refreshes the component. However, there are some situations in an app where a UI refresh needs to be triggered manually to re-render the component.
In Blazor, we use “EventCallback” parameter to emit value from child component to parent component.
Yes, it is possible:
Parent.razor
<Child @ref="_childComponent" />
<button @onclick="@(() => _childComponent.Refresh())">Refresh Child</button>
@code {
private Child _childComponent;
}
Child.razor
<p>Right now: @DateTime.Now</p>
@code {
public void Refresh()
{
StateHasChanged();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With