Blazor: Dispose() is an unfit alternative to the missing OnUnmounted() life-cycle hook.
See original GitHub issueIs your feature request related to a problem? Please describe.
When navigating between pages, the leaving page’s Dispose()
gets called after the OnInit()
of the arriving page. This may cause interference with shared states such as Fluxor’s.
Example: I’ve created a working example at https://github.com/Shammah/DisposeAfterOnInit, most notably on https://github.com/Shammah/DisposeAfterOnInit/blob/master/FluxorFoo/Pages/From.razor and https://github.com/Shammah/DisposeAfterOnInit/blob/master/FluxorFoo/Pages/To.razor
The result is as following:
Describe the solution you’d like
Introduce a proper OnUnmounted()
lifecycle hook that gets called right away when navigating away from a page or unmounted a component, before OnInit()
of the new page or component is called.
Alternatively you could make sure Dispose()
gets called at an earlier stage, but you’d want Dispose()
to remain undeterministic I suppose.
Additional context
My application happens to have multiple Fluxor states with observable subscriptions, which get re-used across many pages. When dealing with observables, it’s best to dispose all subscriptions when they’re not needed anymore. In my case however, it happens that some pages will resubscribe on arrival, except the Dispose()
method of the previous page will immediately dispose the newly created subscriptions, rendering the page unusable. In other words, Dispose()
interferes at the wrong moment with my shared state.
I could work around the problem using reference counters and whatnot, but I think a proper determinsitic OnUmounted()
life-cycle hook would be preferable. This is also in line with other SPA frameworks such as Angular and Vue.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:22
- Comments:8 (4 by maintainers)
Any update on this?
Thanks for contacting us, @Shammah. We’ll consider adding a new lifecycle event in a future release.