Skip to content

using $inspect() or $effect() causes strange behavior #10567

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

Closed
dangelomedinag opened this issue Feb 20, 2024 · 1 comment · Fixed by #10569
Closed

using $inspect() or $effect() causes strange behavior #10567

dangelomedinag opened this issue Feb 20, 2024 · 1 comment · Fixed by #10569
Assignees

Comments

@dangelomedinag
Copy link

Describe the bug

  • if uncomment $effect() or $inpect() counter.count not update
<script>
	let counter = $state({ 
		count: 0,
		increment(){this.count += 1},
	});

	/* Any of the following lines breaks the updates */
	// $effect(() => { console.log(counter) }); 
	// $inspect(counter) 
</script>

<button onclick={counter.increment}>
	clicks: {counter.count}
</button>
  • The following fragment causes a loop when invoking increment
<script>
	let counter = $state({ 
		count: 0,
		increment: () => {
			this.count += 1; /* `this` is undefined obviously */
		},
	});
</script>

<button onclick={counter.increment}>
	clicks: {counter.count}
</button>

clicking causes a loop

Reproduction

link1

link2

Logs

No response

System Info

(Svelte v5.0.0-next.60)

Severity

annoyance

@trueadm trueadm self-assigned this Feb 20, 2024
@trueadm
Copy link
Contributor

trueadm commented Feb 20, 2024

There's some DEV related code that makes your code example work, but really it shouldn't ever work as your event handler has a this of the button element. I'll fix that DEV bit of code, but you should probably avoid passing in the event handler directly and use an arrow function instead:

<button onclick={() => counter.increment()}>
	clicks: {counter.count}
</button>

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

Successfully merging a pull request may close this issue.

2 participants