-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte 5 - being able to tell which props are bound to #10659
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
I'm confused, the docs say:
But I can't trigger the error in REPL (assuming it runs in dev mode). I had no idea that this limitation exists with runes, I have many components where bindings are optional. E.g. a This also refs #8218 because apparently there are now two types of props and not three?
The check would need to happen at runtime and also in production, it's impossible to statically determine if something will be bound. Currently this only happens in dev. |
The docs are outdated, you get a warning (not an error) if you mutate a property (note: reassign is fine) that isn't bound. As for the "check if it's bound" helper: Could you give a concrete example where you need this, where it wouldn't also be possible to reassign the property instead (e.g. |
Thanks, I get it now and all is good! Here's what I learned:
From the wording of their text I'm pretty sure @rChaoz had the exact same misunderstanding of the docs. Throwing in the word "modify" for extra confusion between assignment and mutation 😄 |
Regardless of the docs misunderstanding or not, I still think it would be nice to be able to tell, from a component perspective, when your props are bound or not. |
In what use case would this be actually relevant / benefitial? |
I think my initial use case where you can implement an input field that is read only/editable based on whether or not the "value" prop is bound to is a good example. |
I've been migrating and I know exactly what OP means As of right now, when you edit the input, you get a Runtime Exception. It would be great if this exact error became a compile-time error, with a nice little message saying: "You need to bind to this property, because this component will try to mutate it" There's a problem that I ran into where child would receive props and they wouldn't be reactive at all, they were frozen. The input checkbox would change "checked" (which was bound to a prop), but the prop value would not update at all, I didn't manage to create a simple repro for this problem yet, but I've seen someone on Discord mention running into this problem too, it has to do with passing state props via destructuring |
It's a warning, not an exception, and it only happens during development. You can avoid this in general by not passing objects but primitives. |
@figloalds I think this is not what @rChaoz meant. From what I understand, @rChaoz wants a way to know at runtime "has this property been wired up with The problem with that is that it's not always certain that reassigning a property means that you want the property to be bound - there are also patterns where you may want to go out of sync temporarily with the parent. For example |
@dummdidumm I see In that case, since the internal component is doing this interesting multi-modal functionality, then there should be a way that this component announces that they're doing this, so that we can get a proper warning or error when the parent component uses it wrong. But I do think we should have a bindable thing with the option to configure it to be "Requires two-way binding" or "Can bind, but doesn't require two-way binding", would be nice for my component to announce how it's used, and for the editor to tell me when I'm using a component wrong |
This may be of interest for you: #10768 |
now that #10851 has been merged could this be closed? |
Good point - yes, we can close this now. Additionally knowing inside the component whether or not the property is bound is too niche of a use case to support. It's better to have separate props in this case. |
Describe the problem
Currently, you can always mutate exported let variables (props), regardless of them being bound to or not.
In Svelte 5, you can only modify prop variables if they are bound to. This is a great change, but sometimes you might want to both (1) have props that binding to is optional and (2) modify said props. This is now not allowed. A check before attempting to edit the prop would be very useful.
Describe the proposed solution
There could be something like a
$bound(prop)
statement, which returns boolean. This wouldn't mean any extra runtime/static checks, just a tool for the component builder.Sample use case: custom input field. When
value={x}
is passed, the field is in a read-only state. Whenbind:value={x}
, the field would be editable. A mechanism to indicate which props you can (or even must) bind to would also be nice, but for now I think this alone would be a great addition.Importance
would make my life easier
The text was updated successfully, but these errors were encountered: