question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why the checkbox doesn't work in this reactive inner object?

See original GitHub issue

Describe the bug

Why is the checkbox not working here?

I think Svelte is re-evaluating innerTeam on change, but I don’t understand why: I’m changing innerTeam not team.

I’m using that reactive syntax to change innterTeam only if team changes! Not the other way!

Reproduction

REPL: https://svelte.dev/repl/1ef7657adbc545a0b595e2ab58069b4a?version=3.42.4

  • App.svelte:
<script>
	import {onMount} from "svelte";
	
	import Inner from "./Inner.svelte"
	
	let team = {
		player: {id: "1", name: "John"},
		full: false
	}
	
	$: console.log("team changed:", team);
	
	onMount(() => {
		setTimeout(()=> {
			team = {
				player: {id: "2", name: "Bob"}, full: false
			}
		}, 4000)
	})
</script>

team: <pre>{JSON.stringify(team, null, 2)}</pre>

<Inner {team}></Inner>
  • Inner.svelte:
<script>		
	export let team = undefined;
	
	let innerTeam;
	
	$: innerTeam = {
		...team,
		cars: []
	}
	
	$: console.log("innerTeam changed:", innerTeam);
	
	function addCar() {
		innerTeam.cars = [...innerTeam.cars, {id: "1", brand: "BMW"}]
	}
</script>

<button on:click={addCar}>
	Add car to customTeam
</button>

<br>

<input bind:checked={innerTeam.full} type="checkbox"/> Full?

<br>

innerTeam: <pre>{JSON.stringify(innerTeam, null, 2)}</pre>

Additional

Here a version without Inner.svelte component: https://svelte.dev/repl/6dd87a76d59548559b75f50103980e7a?version=3.42.4.

It’s the same. But if I remove the onMount() it works!

System Info

version=3.42.4

Severity

blocking all usage of svelte

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Prinzhorncommented, Sep 11, 2021

One more thing that might help: you don’t need let innerTeam; at all, $: innerTeam = ... is enough (Svelte will handle defining the actual variable for you). Maybe this helps. Because you don’t declare an innerTeam variable and then make it reactive. You declare a reactive variable and it’s dependencies (“recipe”). See doubled here https://svelte.dev/tutorial/reactive-declarations

1reaction
Prinzhorncommented, Sep 11, 2021

To me personally this behavior makes sense, we both seem to have a different mental model of what the given reactive statement does. And I don’t see it change before a new major version. I personally think what you’re doing is not “correct” (but it’s just an example, not your actual code). What if you add a team.title property in the top level and bind it to a input[type=text]. Now every time you change the title your checkbox is unchecked, because innerTeam is re-created. I’d be more specific about what I want to happen.

But I think we can agree that this is a duplicate?

Edit: my mental model for this example is “here’s my recipe on how to create a innerTeam object, Svelte please make sure it’s always up to date”. And the recipe is complete, nothing else can change the thing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why the checkbox doesn't work in this reactive inner object?
I'm using that reactive syntax to change innterTeam only if team changes! Not the other way! Well not really. You're making a reactive ......
Read more >
Checkbox used v-model doesn't work on a reactive variable
1: Open codepen link 2: Find checkbox used v-model doesn't work with reactive variable 3: But it works with a ref variable ......
Read more >
Checkbox | Angular Material
Current CheckboxRequiredValidator only work with input type=checkbox and does not work with mat-checkbox . Selector: mat-checkbox[required][formControlName] ...
Read more >
Form Input Bindings - Vue.js
Form Input Bindings. Basic Usage. You can use the v-model directive to create two-way data bindings on form input, textarea, and select elements....
Read more >
Angular: Nested Reactive Forms Using ... - Medium
Using Composite CVAs. Pros: Highly Reusable, Portable. Better Encapsulation(Internal Form Controls of the component doesn't necessarily need to be visible to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found