Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i navigate to another blazor page but without state reset

I have a script that does a while loop and does some requests


<p>Result @Content</p>

private string Content;
public async Task StartAsync() 
{
    while(!taskCanel) {  
          Content = await webSocket.SendAsync(..); 
          StateHasChanged();
          await Task.Delay(15000);
    }
   
}

but whenever I switch another page with my navbar then go back to this page it reset everything like it creates a new instance of my razor page.

How can I navigate to the page without losing all states and content?

like image 823
qalooi Avatar asked Aug 30 '25 16:08

qalooi


1 Answers

While you are navigating to another page all variables from the current page should be disposed. It is the standard procedure. The state management works with a different approach. If you want to retain the state of the current page then you have to subscribe to the changes in a global object and pull the change onInit hook or onafterrender hook. There is many more library for state management. Some of them are introduced here

like image 177
Syed Mohammad Fahim Abrar Avatar answered Sep 02 '25 21:09

Syed Mohammad Fahim Abrar