Skip to content
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

Two-way binding for a component makes two updates in a reactive block if bind:value=<object> #3075

Closed
hapcode opened this issue Jun 22, 2019 · 5 comments · Fixed by #5890
Closed

Comments

@hapcode
Copy link

hapcode commented Jun 22, 2019

Primitive types and bindings in native <input> work fine. See REPL.

I added <svelte:options immutable={true}/> and the problem has gone. However I see inconsistent behaviour for <Custom bind:value={object.prop}/> and
<input bind:value={object.prop}/> when immutable is false. Is this a bug?

@lgirma
Copy link

lgirma commented Jun 27, 2019

I am having the same issue. Value changes twice when there is a two way binding in custom components.

In my case, for example, I am having a custom component Pager and using it as:
<Pager bind:myProp={val} />
and doing $: console.log(val) from outside the component logs the value to the console twice. However, doing the logging inside the custom component logs it once.

This doesn't happen when using the binding with a native component like <input bind:value={val}> This has been particularly harmful when there is an http API call when val changes ... which triggers the call twice for no reason.

@jesseskinner
Copy link
Contributor

jesseskinner commented Jul 5, 2019

I'm having the same problem, and in my use case it also causes an unnecessary network request.

I made a REPL before I found this issue.

@jesseskinner
Copy link
Contributor

Oh, I just figured out a workaround for my project, in case it helps someone.

If you want the source of truth on the prop to come from the child component, then leave it undefined in the parent. Then, you can make the reactive variable have a condition on the presence of that variable. eg:

<script>
let prop;
$: prop && console.log(prop);
</script>
<Child bind:prop/>

Might not work for every use case but maybe that helps someone.

@Conduitry
Copy link
Member

This, at last, should be fixed in 3.32.2 - https://svelte.dev/repl/d4df9f70eb34430abde39f7e8d0cd1d1?version=3.32.2 - thanks @pushkine!

@seanthingee
Copy link

@Conduitry @pushkine

I'm not sure if this is fixed yet – or maybe it's a different-but-similar bug that I found?

I've modified the REPL such that the bind:value on the Custom component is customObject instead of customObject.text. I've also added a reactive statement to Custom.svelte to log value. When this is done, it shows custom updates (object) = 2 and the there are two log entries in the console.

https://svelte.dev/repl/a72ad960202241f5af39a90337dafa9d?version=3.38.3

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.

6 participants