-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Potential false positive for ownership_invalid_binding warning
#15727
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
Comments
Sorry just saw there is a merged update for the ownership_invalid_binding that is waiting to be released. |
This is likely not a false positive and the recent fix is unrelated (it pertains to binding fallbacks). The logic for how this warning is triggered and its wording have been changed in #15678. <Sidebar sidebar={sidebarController.leftSidebar} /> This looks like the component is just reading the passed in value, but it is being mutated instead, which could have unintended side effects elsewhere in the application. If these side effects are intended, you are encouraged to make this clear by using a binding instead: <Sidebar bind:sidebar={sidebarController.leftSidebar} /> |
This is a false positive regardless and should be fixed, after https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/internal/client/dev/ownership.js#L34 we're missing a check whether or not the given value is a state proxy (if not we gotta assume it's fine, just like we do in the loop that follows). Reopening |
Can you elaborate on why that is? This seems a bit confusing. |
@brunnerh Even if i do that: <Sidebar bind:sidebar={sidebarController.leftSidebar} /> I just get another error (although not a svelte error) because under the hood the leftSidebar is actually just exposed via a getter 😅 See here: Edit: |
Things like this are why I am not a huge fan of this warning in general 😅 You can also run into this issue e.g. with the {#each items as item}
<Editor bind:item /> <!-- errors -->
{/each} |
I suddenly get very many of these warnings. My use case is the following: https://svelte.dev/playground/c8bdc97915a84239b3de8481ede9076b?version=5.25.10 <script>
const atom = new customStore() // custom class that uses $state() internally, has setter and getter for `value`
const name = view('name', atom, 'unknown') // custom proxy that reads/writes the "name" field from inside the store
// the proxy itself is constant, but it exposes setter/getter named `value`
</script>
<p>Clicking the button causes a warning</p>
<h1>Hello {name.value}!</h1> <-- Outputs the name value, no warning
<input type="text" bind:value={name.value} /> <-- binds the name value, no warning
<Component name={name} />
can not use bind:name because name is const, but the Component shall be able to set name.value=...
but as soon as the setter name.value = is called, svelte reports a warning |
@laszlokorte, In your case the warning is correct, your options are: |
Using let is not a solution because the name variable is not meant to be reassigned it is explicitly meant to be const. The function binding syntax is really verbose. If there would be unexpected behavior caused by my code then the warning should explain that. |
It's not a workaround, it's a statement of intent |
Sorry but I disagree. As can be observed by the correct behavior of my example code, this internal modification of the object is handled correctly by svelte and the $state signal --- as one would expect, since this is the whole point of signals. This counters the claim that data should always only flow downward.
maybe the warning should only be applied to objects/properties that do not define Update: maybe I am simply misunderstanding the warning and the interplay between $bindable and deep mutability. Update 2: I now got my usecase working by using https://svelte.dev/playground/c9a78e6425584356b797a2a63e92890a?version=5.25.10 |
@laszlokorte the warning is trying to prevent you from mutating state that you don't own...in this case the state is created in the parent component and it's better to modify it there be it through a method (like your |
…e cases This fixes an off-by-one error - we did not bail if the top level of the prop wasn't a state already. Fixes #15727
…e cases This fixes an off-by-one error - we did not bail if the top level of the prop wasn't a state already. Fixes #15727
Ok then my point is: why should a setter defined as Will the following example using |
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
I've found a potential false positive for
ownership_invalid_binding warning
I have a class instance in another file that is exported. I hand over said instance to a component.
When i now change an instance prop (which is a state underneath) i get this error.
If it is not a false positive, a quick explanation why is much appreciated.
See REPL for clarity
Reproduction
https://svelte.dev/playground/cb79cd8b0fcf4811a545b187df94e6e6?version=5.25.10
Logs
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: