Skip to content

PersistentState attribute inconsistent behavior on page navigation #63129

@soundaranbu

Description

@soundaranbu

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Scenario 1: Persistent state does not restore after page navigation

  • Create a new Blazor Web App with Interactive Render mode with prerendering enabled(default)
  • Update the counter component as below
@page "/counter"
@rendermode InteractiveServer

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
+    [PersistentState]
+    public int currentCount { get; set; }

+    protected override async Task OnInitializedAsync()
+    {
+       if (!RendererInfo.IsInteractive)
+       {
+           currentCount = 10;
+       }
+    }

    private void IncrementCount()
    {
        currentCount++;
    }
}
  • Launch the application and go to the counter page. It correctly shows 10
  • Now, go back to the home page and come back to the counter page. The count flickers(unnoticable in the recording) and goes back to 0. When I debugged the component, first the component was server rendered and then rendered again in interactive mode on page navigation. But the persistent value was not restored
20250805-1059-20.9857512.mp4

Scenario 2: State restores after page navigation when added a small 1 second delay

  • Now, add a small delay (1 second) before and after setting the counter value
@code {
    [PersistentState]
    public int currentCount { get; set; } = 5;

    protected override async Task OnInitializedAsync()
    {
        if (!RendererInfo.IsInteractive)
        {
+          await Task.Delay(1000);
            currentCount = 10;
+          await Task.Delay(1000);
        }
    }

    private void IncrementCount()
    {
        currentCount++;
    }
}
  • Now, the same state is restored after page navigation
20250805-1111-05.8921160.mp4

This presents inconsistent behavior. My another question was - is it supposed to prerender on page navigation?

Expected Behavior

  • Persistent state should be restored consistently on page navigation

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

10.0.100-rc.1.25401.103

Anything else?

No response

Metadata

Metadata

Assignees

Labels

Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.area-blazorIncludes: Blazor, Razor Components

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions