Closed farfromrefug closed 1 year ago
get_current_component
(or anything else in svelte/internal
) was never part of the public API. I don't know what specific bundler configuration you're using to access functions in svelte/internal
, but we're probably not going to be encouraging any access of that private API.
@Conduitry I understand but still get_current_component is still very useful and I am sure I am not the only one using it (or I would not have found about it).
It is very important to implement methods like onMount
where you would need to listen for the component onDestroy event.
An example is I define a onLanguageChange
which works kind of like onMount
with a callback. The thing is I need to cleanup things when the component which called onLanguageChange
is destroyed. To do that i need to know about that component. Which is where get_current_component gets useful.
Can you understand that get_current_component can be useful for users ?
You can call onMount
or onDestroy
in your onLanguageChange
. It should work.
@j3rem1e thanks a lot ! indeed it works and fills my need!
We use get_current_component
in our svelte library to handle action forwarding
Hi, I have a web component created with Svelte3, used by a container also created with Svelte3 (but the container could be developed using a different framework). The way I used to make them communicate, from component to container, was the following.
<svelte:options accessors="{true}" />
2. MyComponent:
Now moving on Svelte4, this stopped working.
How can I continue to communicate from the component to the container, in an official way?
Thank you
Use the new extend
option to get the host element: https://github.com/sveltejs/svelte/issues/3091#issuecomment-1642351615
Hi, it works perfectly. I changed the code in MyComponent like this:
<svelte:options
customElement={{
tag: 'wm-client',
extend: (customElementConstructor) => {
return class extends customElementConstructor {
constructor() {
super();
this.component = this;
}
};
}
}}
/>
<div >
<MyApp bind:view="{view}" />
</div>
<script lang="javascript">
import MyApp from './MyApp.svelte';
export let component;
let view='';
function view_changed() {
if(!view){
return;
}
component.dispatchEvent && component.dispatchEvent(new CustomEvent('view', {detail: { view:view }}));
}
$:view_changed(view);
</script>
Thank you very much!
@Conduitry on the same note just realized i was also using flush
from svelte/internal
. And obviously now in svelte 4 it gives an error(though it seems to be working). Is there another way to access it?
flush
is an essential part of svelte-native
where there are moments where we must make sure we execute all updates sync in a native (android, ios) function call which needs a return based on the updated components after flush
(we cant wait a tick).
Describe the bug
In svelte 3 we could use
get_current_component
by importing it fromsvelte/internal
Now in svelte 4 it gives the errorCannot find module 'svelte/internal' or its corresponding type declarations
For a while i managed to fix this by using
"moduleResolution": "NodeNext",
in my tsconfig. But now with typescript 5, using"moduleResolution": "NodeNext",
forces me to use"module": "NodeNext",
which completely breaks my app.Is there any better way to import
get_current_component
in svelte 4?Reproduction
add
Logs
No response
System Info
Severity
blocking an upgrade