Skip to content
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

Reactive operator is invoked when a store variable and an assignment to the bound variable are present #7794

Open
mihaon opened this issue Aug 18, 2022 · 2 comments

Comments

@mihaon
Copy link

mihaon commented Aug 18, 2022

Describe the bug

The reactive operator $: is invoked on text input when a store variable (but not local variable) and an assignment to the bound variable are present. This means that I can't just change a local variable to a store variable as part of the refactoring because that will break my code.

Reproduction

App.svelte:

<script>
	import {modal} from './store.js'
	let name = ''
	$: if ($modal.opened) {
		name = 'test'
		console.log('invoked')
	}
</script>

<input bind:value={name}>

store.js:

import {writable} from 'svelte/store'

export const modal = writable({
	opened: true,
})

REPL: https://svelte.dev/repl/624e932ca8d74649a0c8f3a0a65c2c49?version=3.49.0

Steps to reproduce

  1. Type something to the <input>

Expected result

console.log('invoked') is not invoked

Actual result

console.log('invoked') is invoked on every type

But ...

But in the next two examples console.log('invoked') is not invoked.

Example 1

App.svelte:

<script>
	let opened = true
	let name = ''
	$: if (opened) {
		name = 'test'
		console.log('invoked')
	}
</script>

<input bind:value={name}>

REPL: https://svelte.dev/repl/4dc52fa5e027438c9e744fd347789d36?version=3.49.0

Example 2

App.svelte:

<script>
	import {modal} from './store.js'
	let name = ''
	$: if ($modal.opened) {
		console.log('invoked')
	}
</script>

<input bind:value={name}>

REPL: https://svelte.dev/repl/0aa66028eb924709bf4a1d402931fd3c?version=3.49.0

Logs

No response

System Info

REPL from svelte.dev

Severity

annoyance

@fkling
Copy link

fkling commented Jan 13, 2023

I think this is essentially the same issue as #4933

@7nik
Copy link

7nik commented Oct 20, 2023

I believe it's expected behaviour: you change name, the reactive block runs and changes name to "test".

But in example 1, the reactive block is considered static (runs once), which is wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants