Skip to content

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

@frederikhors

Description

@frederikhors

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions