Svelte: onMount and property binding not work correctly

Created on 18 May 2019  路  3Comments  路  Source: sveltejs/svelte

Example:

https://svelte.dev/repl/a4094f9084b248f18d88979c25e1d214?version=3.4.1

let a = 5 is declared before a = 7 runs inside onMount so the expected output value should be 7 both. btw, bind:a does not keep data in sync in this scenario.

bug

Most helpful comment

The main fragment.p() thinks a is still being updated, because it only "finishes" updating on the next tick: add_flush_callback(() => updating_a = false);

For now, you can import { tick } from 'svelte' and use it at the start of your onMount method:

onMount(async () => {
  await tick()
  a = 7
})

REPL

All 3 comments

The main fragment.p() thinks a is still being updated, because it only "finishes" updating on the next tick: add_flush_callback(() => updating_a = false);

For now, you can import { tick } from 'svelte' and use it at the start of your onMount method:

onMount(async () => {
  await tick()
  a = 7
})

REPL

I made a related REPL before I found this issue. My workaround was to also add the property to the child component as well, eg.

https://svelte.dev/repl/3c8fe4fb50984f70a45b38b894d62a35?version=3.9.0

Was this page helpful?
0 / 5 - 0 ratings