Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor layout gets re-created on page navigation?

$
0
0

Whenever I click on a <NavLink> in Blazor (Server and WASM, doesn't matter which) my entire layout gets re-created, not just the @Body/@page component.

My understanding is this happens because the layout is inside the <Router> and the router just re-creates the whole thing on page changes. The problem with that is that I lose my sidebar's state whenever I navigate (the OnInitialized() method gets called).

I've found this GitHub post (which is not directly related to my issue) where Microsoft's solution is to essentially move the layout outside the router, but then you're essentially stuck with an app-wide "layout". On top of that, all the documentation and tutorials I can find never mention this problem.

I'm sure I'm just missing something simple here. Surely Blazor's router is smart enough to only re-create the @BodyRenderFragment and preserve the layout's current state, no? I've seen plenty of sites where navigating doesn't re-render the whole app. MudBlazor's documentation page is one and looking their documentation's source code, they don't seem to be doing anything special with their layout.

Am I supposed to do something more complicated than this?

<Router AppAssembly="@typeof(App).Assembly"><Found Context="routeData"><AuthorozeRouteView Resource="routeData"                             RouteData="routeData"                             DefaultLayout="@typeof(MainLayout)"/></Found><NotFound><PageTitle>Not found</PageTitle><LayoutView Layout="@typeof(MainLayout)"><p role="alert">Sorry, there's nothing at this address.</p></LayoutView></NotFound></Router>

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>