\n\n
\n```\n\nservice.svelte.js\n```js\nimport { setContext, getContext, mount } from 'svelte';\nimport NestedComponent from './NestedComponent.svelte'\n\nexport const contextTest = (target) => {\n\tconst stateObject = $state({\n\t\tshowText: true\n\t})\n\tmount(NestedComponent, {target, context: new Map([['stateContext', stateObject]]), props: {}})\n\t\t\n setInterval(() => {\n\t\tstateObject.showText = !stateObject.showText\n\t}, 1000)\n}\n```\nNestedComponent.svelte\n```html\n\n

Following text is inside an 'if' statement and should only ever be able to show 'true'

\n\n{#if stateObjectFromContext.showText === true}\n

{stateObjectFromContext.showText}

\n{/if}\n```\n\n### Logs\n\n```shell\n\n```\n\n### System Info\n\n```shell\nSystem:\n OS: Linux 5.15 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)\n CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H\n Memory: 9.89 GB / 15.49 GB\n Container: Yes\n Shell: 5.1.16 - /bin/bash\n Binaries:\n Node: 22.2.0 - ~/.nvm/versions/node/v22.2.0/bin/node\n Yarn: 1.22.22 - /usr/bin/yarn\n npm: 10.7.0 - ~/.nvm/versions/node/v22.2.0/bin/npm\n pnpm: 10.10.0 - /usr/bin/pnpm\n npmPackages:\n svelte: ^5.28.2 => 5.28.2\n```\n\n### Severity\n\nblocking all usage of svelte","author":{"url":"https://hamdit.com/proxy/.com/kepzAT","@type":"Person","name":"kepzAT"},"datePublished":"2025-05-06T14:31:38.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://hamdit.com/proxy/schema.org/CommentAction","userInteractionCount":8},"url":"https://hamdit.com/proxy/.com/15870/svelte/issues/15870"}
Closed
@kepzAT

Description

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:

{#if value === true}
    {value}
{/if}

but the rendered HTML shows both "true" and "false".

Some more observations

  • Duplicating this if block in the template twice makes it work
  • There are problems with $derived not reacting to changing state as well

Reproduction

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}

Logs

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
    Memory: 9.89 GB / 15.49 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 22.2.0 - ~/.nvm/versions/node/v22.2.0/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 10.7.0 - ~/.nvm/versions/node/v22.2.0/bin/npm
    pnpm: 10.10.0 - /usr/bin/pnpm
  npmPackages:
    svelte: ^5.28.2 => 5.28.2

Severity

blocking all usage of svelte