Skip to content

[Blazor] NavigationManager.HistoryEntryState parameter binding with [SupplyParameterFromHistoryState] #43962

@hakenr

Description

@hakenr

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I am trying to use the new HistoryEntryState (#42617) for preserving state of single page (component), let's say query text (but it can be page number for the grid, filter settings, sorting settings, whatever state of the page, which I decide not to put in the query-string, but use HistoryEntryState instead).

I create several new entries in the navigation history, with the same path (I keep navigating to myself with new state). When I click Back in the browser history, the application navigates back (to the same path, but different HistoryEntryState), but the component does not get any signal that there the state is changed thus does not go through the change-render phase (no OnInitialized, no OnParametersSet, no nothing).

Describe the solution you'd like

One solution might be to handle HistoryEntryState changes similarly to the QueryString changes, where there is the [SupplyParameterFromQuery] attribute, which you can use for a [Parameter] to get the value from query and track receive the changes.

Something like [SupplyParameterFromHistoryEntryState] might work.

Additional context

Simple demo:

@page "/history-entry-state/search"
@inject NavigationManager NavigationManager

<HxInputText Label="Search query" @bind-Value="query" InputSize="InputSize.Large" />
<HxButton Text="Search" OnClick="HandleSearchClick" Color="ThemeColor.Primary" />
Dsplaying results for the query: @query

@code {
    private string? query;

    // this does not work as there is no change in the parameters
    protected override void OnParametersSet()
    {
        // there is no way to receive the changes when navigating back on the same page with different history entry state
        query = NavigationManager.HistoryEntryState;
    }

    private void HandleSearchClick()
    {
        NavigationManager.NavigateTo("history-entry-state/search", new NavigationOptions() { HistoryEntryState = query });
    }
}
  1. Navigate to the page
  2. Enter "first" to the input and click Search button.
  3. Enter "second" to the input and click Search button.
  4. Click Back in the browser.
    ...the browser navigates to the same path with history entry state "first", but there is no chance for the page-component to act and rerender (in opposite to passing the query value in query-string and using the [SupplyParameterFromQuery]).

We might conclude that you should preserve such state in query-string and history entry state is not the right place for such state. I just want to point out that there is no straightforward way to use the history entry state in the components. Should we pass the history entry state to the components as CascadingValue direcly from ...., from where?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-navigation

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions