-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Components added to dom using 'mount' have broken reactivity in {#if} when reacting to state from context #15870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I can narrow it down more, in this Playground it doesn't work if you modify the state from inside |
That is not how to set context when mounting components dynamically. const stateObject = $state({
showText: true
});
- setContext('stateContext', stateObject);
mount(NestedComponent, {
target,
props: {},
+ context: new Map([['stateContext', stateObject]]),
}); If you want to pass on existing contexts, use (By the way, you overwrote the state of the original reproduction, so it is unclear what the original issue looked like. I would generally recommend including code in the issue rather than just linking to the playground.) |
My bad for accidentally overwriting, reverted to the example! And here is the code: App.svelte <script>
import { setContext, getContext, onMount } from 'svelte';
import { contextTest } from './service.svelte.js';
const stateObjectFromContext = getContext('stateContext')
let element = undefined
onMount(() => {
contextTest(element)
})
</script>
<div bind:this={element}></div> service.svelte.js import { setContext, getContext, mount } from 'svelte';
import NestedComponent from './NestedComponent.svelte'
export const contextTest = (target) => {
const stateObject = $state({
showText: true
})
mount(NestedComponent, {target, context: new Map([['stateContext', stateObject]]), props: {}})
setInterval(() => {
stateObject.showText = !stateObject.showText
}, 1000)
} NestedComponent.svelte <script>
import { setContext, getContext } from 'svelte';
import { ContextTest } from './service.svelte.js';
const stateObjectFromContext = getContext('stateContext')
</script>
<p>Following text is inside an 'if' statement and should only ever be able to show 'true'</p>
{#if stateObjectFromContext.showText === true}
<h1>{stateObjectFromContext.showText}</h1>
{/if} With your suggestion it still does not work for me unfortunately. |
Strange bug. Adding an |
Wrapping |
I don't think adding another Doing some playground bisection, the issue seems to have been introduced in v.5.24.0 Playground @ 5.23.2 Maybe it has to do with this change: Seen some other issues referencing that in particular. |
Describe the bug
If you mount a component to the DOM programmatically using
mount
, and pass in a$state
object via context. reactivity to the state with{#if}
statements does not work correctly.Playground:
https://svelte.dev/playground/63a37aca753544ba9c2f96dd5bd4cd3a?version=5.28.2
In this snippet I show something like:
but the rendered HTML shows both "true" and "false".
Some more observations
Reproduction
App.svelte
service.svelte.js
NestedComponent.svelte
Logs
System Info
Severity
blocking all usage of svelte
The text was updated successfully, but these errors were encountered: