-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte 5: Add property change hook #10236
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
Could you give more information why you need this / examples where this would be useful? |
One example was given in #9944, where people tried to reproduce the required behavior with both I have usually run into this when properties have dependencies amongst themselves, often related to validation or coercing ranges via a minimum/maximum. Or instead of a range, a certain format has to be ensured, so if a value is set on the component from the outside, the UI has to be updated, but on user interaction that should not be the case, as it would interfere with typing. Even if you allow the interference, if you try to do this via reactivity, having an underlying and a formatted value, you run into circular dependencies or, in the case of Svelte 5, infinite effect loops. There are some ways around this, but they are unintuitive and complicated. |
In v5, today, here a missing link between bind:prop in parent component and same prop in child component.
edit: And after using $effect, I fully understand that $effect is not a replacement for I tried to write about my experience with $effect, but it was closed. #9944 onPropChange() is something like afterUpdate - right? Solutions from other frameworks:
|
Hey maybe I’m a bit confused here but when bounding props from parent to child isn’t your default value going to be defined in the parent since that’s the flow of the variable? Parent -> child. If you want a default value in child assign that in parent since it’s being passed in? |
bind:value is a two-way binding |
For my own interest, I had a go at implementing the example use case with $effect(): <script>
let { values = $bindable(), disabled = $bindable(), maxLength = $bindable() } = $props();
$effect(() => {
if (!values.length) return;
if (disabled) values = [];
if (values.length > maxLength) values = values.slice(0, maxLength);
});
</script> End result is ok, but debugging infinite loops isn't a great dev experience 😅. |
@dummdidumm The problem I'm having is that the effect is resetting the location to the selected color when you click next so it ends up not showing the next list of options. Perhaps I'm thinking about the state wrong and I'd love to know how to think properly about it if so. |
Sorry, I'm going to rescind the need for this request as I figured out that I had more state variables in my component than needed and the old structure allowed for it. After refactoring the variables, I found the problem went away. I think the way to describe it was that I was linking state. I appreciate the consideration though! |
Uh oh!
There was an error while loading. Please reload this page.
Describe the problem
It was already a pain to enforce consistent internal component state in v4, it's now even harder with v5 because
$effect
will cause infinite loop issues.$derived
is not going to solve this, it serves a different function entirely.Describe the proposed solution
Add a hook that runs on external changes to the props, the callback inside will not be an effect so any changes to or normalization of internal state or props should not cause the handler to re-run. E.g.
The event object could be supplied with the name of the property that was changed, and maybe even old and new values.
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: