How to manually mount()
and $bindable
#16675
-
https://svelte.dev/docs/svelte/$bindable
https://svelte.dev/docs/svelte/svelte#mount How to implement |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
A bindable property is identified by having If the You need a getter if you want the mounted component to receive updates by referencing reactive state. Example: <script>
import Input from './Input.svelte';
import { mount } from 'svelte';
let noBindingValue = $state('Text 1')
let bindingValue = $state('Text 2');
function noBinding(target) {
mount(Input, {
target,
props: { get value() { return noBindingValue } },
});
}
function binding(target) {
mount(Input, {
target,
props: {
get value() { return bindingValue },
set value(v) { bindingValue = v },
},
});
}
</script>
<h2>No binding</h2>
<div {@attach noBinding}></div> {noBindingValue}
<h2>Binding</h2>
<div {@attach binding}></div> {bindingValue} |
Beta Was this translation helpful? Give feedback.
A bindable property is identified by having
get
&set
accessors.If the
set
is missing, the update will only be local to the component, otherwise the value can be propagated through theset
accessor to some other piece of state.You need a getter if you want the mounted component to receive updates by referencing reactive state.
Example: