Home > OS >  Force Blazor component to rerender when url query changes
Force Blazor component to rerender when url query changes

Time:05-31

Everything I've read points me to using NavigationManager.LocationChanged event, however that does not detect when only the query string changes. Therefore, nothing fires.

I've tried what is suggested here and that does not do what I am looking for.

I need my component to rerender everytime the query parameters in the current route changes. Has anyone been able to do this?

CodePudding user response:

You can bind the StateHasChanged() to the function/logic which changes your query string, which will re-render your component.

CodePudding user response:

I do this in a wasm app works well

set this on your page where you want to receive the route parameter

@page "/pagename/{text?}"

<div>@Text</div>

then

@code {
    [Parameter]
    public string? Text { get; set; }

    protected override void OnInitialized()
    {
        Text = Text ?? "fantastic";
    }
    protected override void OnParametersSet()
    {
        Text = Text  ?? "Name Not Supplied";
    }
}

The will re-render any data changes, AFAIK you can't force a component to re-render without reloading it, only the changed subscribers, which makes sense, as re-renders are expensive.

Doc's look under router parameters

  • Related