Aspnetcore: Detect Component Unload Event - OnUnloadAsync()?

Created on 30 Jul 2018  路  7Comments  路  Source: dotnet/aspnetcore

I think there needs to be some way to detect when a component is unloaded so that any code that's running in the background can appropriately check whether the components exists.

As it is in experimenting with a background timer, I ended up creating a component that never releases due to a circular reference and there's no way to check to see whether resources should be released when the component unloads.

area-blazor

All 7 comments

Dispose component not helps?

Hi, I have been using IDispose too for releasing a Timer and it works well as long as you鈥檙e not creating component instances in a loop: see the this issue aspnet/Blazor#1065 .

Thanks guys - that totally works.

Would be nice to have a more obvious way to do this though with a an event hook that shows up in the override list though.

Having the same issue in 2019 still using Blazor 0.9.0! using a foreach loop to create a bunch of components each implementing IDisposable. Using custom paging. Move to next page the first page will randomly dispose around 50 to 60% of its components but never all of them. Return to that page and it will then recreate only those ones that it disposed of as the others still exist. Any suggestions on how to effectively dispose of all components created in a loop?

Try to set new unique ID to all component and update it each time you change page index.

Old components are not removing because blazor internal engine when calculates diffs thinks that they are not touched, this is bug obvious but to force it you need to set new IDs, in result it will remove old ones from dom because old components with old id will not be in list anymore.

ID I mean for top html element in your component and unique id you can get easy using GUID
Guid.NewGuid().ToString("d").Substring(1, 4)

Great suggestion thank you.

Was this page helpful?
0 / 5 - 0 ratings