Skip to content

Svelte 5: binding an object to a prop has no effect #10779

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
harrisi opened this issue Mar 12, 2024 · 1 comment
Closed

Svelte 5: binding an object to a prop has no effect #10779

harrisi opened this issue Mar 12, 2024 · 1 comment

Comments

@harrisi
Copy link

harrisi commented Mar 12, 2024

Describe the bug

When passing an object to a child prop, the child can update it as if it were bound, even without using bind:. When not using an object, bind: is required.

Reproduction

Svelte 5 REPL EDIT: This might be a better REPL to show that there's no difference.

Svelte 4 REPL

Logs

No response

System Info

REPL

Severity

annoyance

@Conduitry
Copy link
Member

This is a combination of intended behavior and a known limitation with a runtime check.

You are discouraged from mutating state that has been one-way passed into your component. Your original Svelte 5 REPL logs a warning about this at run time in dev mode. (It won't in prod mode so we can avoid the overhead of checking for these mutations at runtime.)

In your revised Svelte 5 REPL, you are both passing it in as a one-way prop and passing it in as a two-way binding to different instances of the child component, and there isn't a good way at runtime for the first instance of the child component to know that it shouldn't own the counter variable while the second one should.

In the Svelte 4 REPL, the parent component's counter value is still mutated (which can be seen by adding something like setInterval(() => console.log(counter), 1000) to the parent component), but it just doesn't find out about it because of a limitation of the compile-time dependency tracking.

@Conduitry Conduitry closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2024
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

2 participants