Home > Software engineering >  How can i navigate to another blazor page but without state reset
How can i navigate to another blazor page but without state reset

Time:12-07

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?

CodePudding user response:

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

CodePudding user response:

You need to separate your data and your presentation. Your data, how you get it and manage it belongs in a scoped DI service. Your UI accesses this DI Service through inject and displays information from the service.

  • Related