-
Given the following code: <script>
import {getContext} from "svelte"
let getData = getContext('data');
let data = getData();
const handleInput = (e)=>{
data = e.target.value;
}
</script>
<input oninput={handleInput}/>
<p>should not change, because pass by value? {data}</p> I expect the data can't update the view when it is updated, but it will. The JS output is let data = $.mutable_source(getData()) And if I add some code import {getContext} from "svelte"
let getData = getContext('data');
let data = getData();
+ let foo = $derived('');
const handleInput = (e)=>{
data = e.target.value;
} The warning and the data won't update the view when it is updated. The JS output is let data = getData(); Is this a bug, or am I missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This isn't about context, this is because Svelte compiled your code in legacy mode in the first example. Currently, Svelte will default to compiling your code in legacy mode if it doesn't see any Svelte 5 features being used (eg runes). In legacy mode, all top level variables that are reassigned are implicitly reactive, which is why |
Beta Was this translation helpful? Give feedback.
This isn't about context, this is because Svelte compiled your code in legacy mode in the first example. Currently, Svelte will default to compiling your code in legacy mode if it doesn't see any Svelte 5 features being used (eg runes). In legacy mode, all top level variables that are reassigned are implicitly reactive, which is why
data
was reactive in the first example.