question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor: Dispose() is an unfit alternative to the missing OnUnmounted() life-cycle hook.

See original GitHub issue

Is 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: image

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:closed
  • Created 4 years ago
  • Reactions:22
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
rborosakcommented, Nov 21, 2019

Any update on this?

8reactions
mkArtakMSFTcommented, Aug 12, 2019

Thanks for contacting us, @Shammah. We’ll consider adding a new lifecycle event in a future release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I properly dispose a component in Blazor?
You can use Dispose method to release unmanaged resources, unhook events, dispose DotNetObjectReference instances to avoid memory leak. Refer to the following ...
Read more >
Blazor component removed from DOM before disposal ...
The problem is that by the time the js code runs, the component is already destroyed - so the DOM element is missing,...
Read more >
ASP.NET Core Razor component lifecycle
This article explains the ASP.NET Core Razor component lifecycle and how to use lifecycle events. The Razor component processes Razor ...
Read more >
Blazor Components Deep Dive - Lifecycle Is Not Always ...
Get insights into the lifecycle of Blazor components. Addressing the easy, misleading, and complex parts about Blazor component lifecycle.
Read more >
Component Lifecycle in Blazor WebAssembly .NET 6
Component has 4 main phases: Initialize, ParametersSet, AfterRender and Dispose. Each phase has hooking methods with different purpose, allows you to manipulate ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found