Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh a blazor sub/child component within a main/parent component?

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?

like image 887
001 Avatar asked Dec 07 '19 14:12

001


People also ask

How do you refresh a child component in Blazor?

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.

How do I automatically refresh a component in Blazor?

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.

How pass data from child component to parent component in Blazor?

In Blazor, we use “EventCallback” parameter to emit value from child component to parent component.


1 Answers

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();
    }
}
like image 59
user3071284 Avatar answered Sep 20 '22 08:09

user3071284