I'm going crazy trying to persist data over the pre-rendering phase using PersistentComponentState. #54824
Replies: 2 comments 2 replies
-
Hello @GlacierFox, were you able to get this one figured out? I'm having the same issues and haven't been able to resolve it yet. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Hi. @rmunson8 I've basically got a small snippet of code in my There's probably a better way of doing this but here's what works for me. I created a simple class to hold small bits of data I'd like to carry down to all components which I can set in my
I then created a base Cascading Value in my
To set the actual data I add this to my
I can carry that down into any component. So I add this to my MainLayout.razor to have access to the data I've set in my App.razor. But I can't use this yet as it will null out over the double rendering phase.
Persisting data over the pre-rendering phase.Now, to allow me to transfer my IP data over the double rendering phase so it doesn't null out in my
Create a function to persist your data when the component loads:
Finally, in
I can now use it in my |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've got a bit of data in a root level cascading value which I've cascaded down into a component which needs to be retained over the pre-rendering phase. I's just a simple string which is generated inside my
App.razor
which is cascaded down for general use.Anyway, I've scrapped that because I couldn't get it to carry over the pre-rendering phase in any component which consumed it. After adding a breakpoint, everything is all fine on the initial cycle through
OnInitialized()
but I couldn't get the data to cache and restore on the second run through so it would just null out and disappear.I thought I'd go and try and get a minimal component working to see if I'm going insane by literally lifting example code from the Prerender ASP.NET Core Razor components page on the documentation site.
I can't even get that to work. 😵💫
I've lifted this section of code which is supposed to simply demonstrate carrying a value over past the pre-render stage so I've put it as a new page on the side bar in a new Blazor Web App project along with the existing counter and weather pages.
(I'm using the Fluent Blazor components by the way)
I'm supposed to get two identical results at the bottom - but I only get one.
There's a note on the page which says :
Okay so this component is navigated to from my NavBar on the left and makes used of interactive routing (I think) so there's no pre-rendering and
OnInitialized()
is only cycled through once, which is correct because I've checked with a breakpoint. The value will only change if I reload - which it does. Of course this entire code segment serves no purpose if the page doesn't re-render. 🤔The thing is, my actual component I'm using in my app makes use of
@rendermode InteractiveServer
soOnInitialized()
fires twice ('ve checked with a breakpoint), even when navigating to it in the same way (using interactive navigation?) from the NavBar on the left.So I've added
@rendermode InteractiveServer
to the above code and I'm getting the same problem I'm having in my main project. After putting a break point insideOnInitialized()
, the initial data is loading in which is great and the data is being displayed on the page and the value is successfully stored inside theApplicationState
because I can see it in there.The problem is, after resuming the execution, on the second cycle through
OnInitialized()
, it seems there's nothing in theApplicationState
to extract and restore. It just gives me a new random number, it doesn't actually restore the value that has been stored so I get;It's going though the false side of the for-loop again even though the value is supposed to be stored in there from the previous run through
OnInitialized()
.Am I misunderstanding something about how this is supposed to work? This bit at the bottom of the documentation page may have something to do with it but I don't think I'm fully understanding it...
If pre-rendering doesn't occur when (I'm assuming) I'm using interactive routing, why does it cycle through
OnInitialized()
twice and wipe out my data?Update - It's not working because of enhanced navigation. I've just manually pasted the URL into my browser and everything is working fine and its keeping state if I do it this way. If I was to navigate to it using the Nav Bar I still get the same problems which I'm yet to find a solution for. I should have read this bit more carefully:
Beta Was this translation helpful? Give feedback.
All reactions