-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Event "destroy" not exist in Svelte3 anymore. #3089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
A fix for #19 Hopefully temporary, until sveltejs/svelte#3089 can be resolved in a more satisfying way.
A fix for #19 Hopefully temporary, until sveltejs/svelte#3089 can be resolved in a more satisfying way.
The difference in Svelte 3 is that component's can't self-destruct — there's no equivalent of Inside a component, you can use What's a (concrete) scenario in which this is necessary? |
Current workaround with a one-liner (+import) for each component that needs an observable destroy event (using public api): https://svelte.dev/repl/689a66f5a7af4f498a7d4519a63c49af?version=3.5.4 // Comp.svelte
useDestroyEvent();
// other.js
export const useDestroyEvent = () => {
const dispatch = createEventDispatcher();
onDestroy(() => dispatch('destroy'))
} |
comp.$$.on_destroy.push( ()=> console.log('asda')) |
svelte-state-renderer now has a PR to simply call the appropriate code upon destroying the component - TehShrike/svelte-state-renderer#21 As I understand it, this ASR bug was why this issue was opened in the first place. Is there another reason to have this?
|
component.$destroy() is it atomic operation or it's the async method? |
@Rich-Harris I need something like this, but if I run onDestroy outside I get next error:
probably because I am using splitting code and this js contains in the separate chunk. |
hmm for me onDestroy not working at all. |
Sorry, it's happening because it was in my rollup config:
but in your store.mjs you make local import (from '../internal') in that case rollup didn't check package.json and firstly try index.js after start working commonjs plugin and dublicate set_current_component method.
|
Closing this. As Rich mentioned, the only way a component can be destroyed now is if someone destroys it. Re-open if you have a specific case where this event is necessary and you can't just emit it yourself. |
Hey @Conduitry. It crossed my mind that there might be a use case for this, relating to third party libraries. It's quite common to use a third party library to do something for you in the DOM. For instance, Choices.js or Material Datetime Picker. It's also quite common to wrap the functionality of those libraries in your own generic components, for use in a lot of places, where the consumers don't need or want to worry about the complexity within those components, particularly that concerning the interaction with the third party library. In such cases, it can often be important to handle the generic component being destroyed. Otherwise, the library might malfunction or exhibit a memory leak, unaware that its elements have been removed from the DOM. If it is possible to listen for a Such manual handling would seem to be problematic in several regards. First, the handling could be quite complex, depending upon the nature of the functionality of the generic component. It might be thought that consumers should not be exposed to such complexity. Second, it would seem to constitute unpleasant boilerplate, required for each and every instance in which the generic component is used. Third, it would seem error-prone, as a consumer could easily forget to implement the boilerplate. I've used |
@ChrisTalman In the case of wrapping components, I tend to use the onMount return value (onDestroy). This will be called if Svelte removes third party componenents from the DOM at any point. import { onMount } from 'svelte'
import SomeComponent from 'some-thirdparty-js'
let thirdParty
onMount(() => {
thirdParty = new SomeComponent()
return () => thirdParty.destroyMethod()
}) |
@antony Nice, thanks. So, that is a |
@ChrisTalman |
@stalkerg can you explain the scenario where you need destroy outside the component tree more clearly please? I've migrated 30 + components and 2 of 3 apps to Svelte v3 from v2 and I've not yet encountered this issue. I'd like to understand your use case (ideally with a REPL). |
Hi @Conduitry Storybook addons use decorators to wrap the user story inside some wrapper component. That code still contains legacy code for svelte 2 that does not work anymore: component.$on('destroy', () => {
wrapper.$destroy(true);
}); The problem is that the I've tryed the solution proposed by @timeshift92 by replacing the above 3 lines of code with My knowledge on Svelte is not sufficient here to solve the problem. |
I can tell you, sometimes it would be a wonderful thing if we could make sure all slots in a thing had their on Destroy called before the parent one was called. I'm looking at you mapbox-gl. |
I found one of important lack of functionality during porting my code to svelte3 from the second version. Looks like we have no "destroy" event outside of component anymore. It's an important part of the public component API.
Basically, we have $on the method but without the "destroy" event it's useless
Also, we should have some extra lifecycle hooks outside of the component, it's important for routing and some other cases.
The text was updated successfully, but these errors were encountered: