5 ms·
top level variables are reactive even when updated inside a function, just give it a try: https://svelte.dev/repl/44e3aece18294ddb834bcdfb4394af48?version=3.57
by opensas 3y ago
top level variables are reactive even when updated inside a function, just give it a try:
https://svelte.dev/repl/44e3aece18294ddb834bcdfb4394af48?version=3.57.0 https://svelte.dev/repl/44e3aece18294ddb834bcdfb4394af48?ver...
`
<script>
let name = 'world';
const flip = () => name = name === name.toUpperCase() ? name.toLowerCase() : name.toUpperCase()
$: console.log({ name })
</script>
<h1>Hello {name}!</h1>
<button on:click={flip}>flip!</button>
`
- opensas 3y agothe inconsistency comes from the fact that this reactivity, triggered from inside a function, is not taken into account to build the dependency graph and ultimately decide in which order to process the statements