-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Description
Description
I think the Factor out the state preservation to a common location section has some issues that make it incomplete or problematic:
-
Prerendering Issue:
The provided example fails during prerendering becauseProtectedSessionStore.GetAsynccannot be invoked fromOnInitialized. To fix this, prerendering must be disabled for the example to function correctly. See this code. A possible solution is changing the render mode to something likenew InteractiveServerRenderMode(prerender: false). -
Issue with Multiple Subscribers:
TheCounterStateProviderfails when more than one component subscribes to[CascadingParameter] private CounterStateProvider? CounterStateProvider. The cascading value in this case is theCounterStateProvidercomponent instance (this), which doesn't change after being set (you could even setIsFixed="true"). Since the actual counter value (CurrentCount) changes internally, theCascadingValuecomponent is not being re-rendered (allowing the new value to be distributed to subscribers) and updates toCurrentCountare not propagated to other components subscribed to the cascading value. (The only situation whenCascadingValuecomponent notifies the subscribers of changes is when theCascadingValueitself receives new parameters inSetParametersAsync.)Steps to Reproduce:
- Follow the documentation steps, fixing the prerendering issue as mentioned above.
- Create a new
SubCounter.razorcomponent, duplicating the content ofCounter.razorbut without the@pagedirective. - Add
<SubCounter />toCounter.razorso both components render simultaneously. - Test the two Increment buttons (one in
Counter.razorand the other inSubCounter.razor). You'll notice that updating the counter in one component doesn't trigger updates in the other.
The current implementation of
CounterStateProviderdoesn't include a mechanism to notify subscribers about state changes. Without such notifications, this approach is effectively just another way to inject a service into components. If the intent is to use cascading values/parameters for proper state management, the "state" (e.g.,CurrentCount) should be the cascading value distributed, not the "provider" (e.g., theCounterStateProvidercomponent).Suggestion for Improvement:
To use cascading values/parameters effectively for state management, we could useCascadingValueSource<T>. This pattern is used in Blazor's built-inAuthenticationStateProvider, as seen in the AuthenticationStateCascadingValueSource implementation. By propagating the actual state (CurrentCount) rather than the provider, changes would trigger proper re-renders of subscribed components.
(I can try proposing new content using the CascadingValueSource, but it'll probably take some time to prepare since I'm pretty busy with other projects right now.)
Page URL
Content source URL
https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/state-management.md
Document ID
e5e1273b-195e-5da1-b4aa-66bbcff1425b
Article author
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
