What is the purpose of PersistentComponentState and how do you use it to pass state from server-side prerender to the client-side interactive component?
Last updated
PersistentComponentStateserializes state during server prerender into the HTML, so the interactive client component rehydrates it instead of fetching again (no double-fetch or flicker).Save via
RegisterOnPersistingwithPersistAsJson(key, value)inside the callback. Read on the client withTryTakeFromJson(key, out value)and fetch only if it fails.Dispose the subscription to avoid leaks. It is a one-shot handoff, not ongoing sync.
- https://learn.microsoft.com/en-us/aspnet/core/blazor/state-management/prerendered-state-persistence?view=aspnetcore-10.0
- https://alexb72.medium.com/prerendering-in-blazor-how-to-persist-state-using-persistingcomponentstate-6a1ead059aa0
- https://d20tek.com/articles/blazor-persistent-component-state
- https://www.telerik.com/blogs/blazor-basics-prerendering-server-components-blazor
- https://jonhilton.net/persist-state-between-renders-net8/
- https://daily.dev/posts/z8v0ynvdn
- https://juliocasal.com/blog/dealing-with-blazor-prerendering
- https://devinstance.net/blog/handle-prerendering-right-in-blazor