Reputation: 4156
I have this simple situation in my Svelte project:
REPL: https://svelte.dev/repl/41112edc53e04d59ba9a824cc9e6cde3?version=3.48.0
<script>
import Comp from "./Comp.svelte";
let obj = {hello: 'world'};
$: console.log("obj in App:", obj)
</script>
<Comp bind:obj></Comp>
<script>
export let obj;
</script>
<h1>Hello {obj.hello}!</h1>
What I don't understand is why the obj
in App.svelte is changed from the Comp.svelte
the first time it renders.
You can see in the log:
I expect it not to be changed until I change it somehow.
Upvotes: 1
Views: 79
Reputation: 185410
I think the logic is like this:
obj
property of <Comp>
causes the property to be invalidated therebind:obj
, an invalidation of the property causes an invalidation in <App>
as well.If you use <Comp {obj} />
instead, this does not happen.
In general I would not recommend relying on the precise number of times that reactive statements are called.
Upvotes: 4