Replies: 1 comment 1 reply
-
<script>
import {setContext} from "svelte";
const {children} = $props();
let currentValue = $state();
let setOfValues = $state([]);
const context = setContext("ctx", {
get setOfValues() { return setOfValues; },
get currentValue() { return currentValue; },
set currentValue(newVal) { currentValue = newVal; }
});
$effect(() => {
console.log(context.setOfValues)
})
</script>
{context.currentValue}
{@render children()} The above works. The problem: You're simply returning a copy of the value. Why? Because Still, I'd recommend not doing this, and instead creating a class that encapsulates the whole thing. The compiler will then create your get and set accessors for you. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Changing context.currentValue in the child component doesn't update the parent context.
Simple repl:
https://svelte.dev/playground/6d90fb3ac80b4c3fa37d8445402e1980?version=5.25.3
Beta Was this translation helpful? Give feedback.
All reactions