-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat: add support for bind getter/setters #14307
Conversation
🦋 Changeset detectedLatest commit: 0a7608e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
preview: https://svelte-dev-git-preview-svelte-14307-svelte.vercel.app/ this is an automated message |
|
Nice! One piece of feedback, I think it would be helpful to be able to define a getter and setter outside the Maybe in addition to or instead of allowing using an object with get / set as properties or an array of 2 functions. And, at the same time allow using an object with get and set functions: <script>
import Child from './Child.svelte';
let a = $state(0);
const accessorsA = {
get () {return a},
set (v) {
console.log('a', v);
a = v;
}
};
</script>
<button onclick={() => a++}>a: {a}</button>
<Child
bind:a={accessorsA}
/> using an array with 2 functions <script>
import Child from './Child.svelte';
let a = $state(0);
const accessorsA = [
() => a,
(v) => {
console.log('a', v);
a = v;
}
]
</script>
<button onclick={() => a++}>a: {a}</button>
<Child
bind:a={accessorsA}
/> Currently it's doable of course if you define 2 functions or use a destructuring assignment from an array but it's kind of annoying: <script>
import Child from './Child.svelte';
let a = $state(0);
let [getA, setA] = [
() => a,
(v) => {
console.log('a', v);
a = v;
}
]
// or
getA = () => a;
setA = (v) => {
console.log('a', v);
a = v;
};
</script>
<button onclick={() => a++}>a: {a}</button>
<Child
bind:a={getA, setA}
/> Personally, as far as being very intentional and explicit, I would prefer an object with a Also, currently getting a Svelte warning If you change the value in the input, this warning is shown:
Playground: ownership_invalid_mutation If the getter and setter in the parent only expose a particular property of the nested object then it works without a warning, |
I mean you can do Doing it this way — with syntax — is better:
The warning looks like a bug, will investigate |
fixed the warning by hoisting the code that adds the child component as an owner of the state. to do that I changed the internal representation from an |
yeah, great points, especially about the runtime overhead, definitely compile time is the best option. As it stands, using it this way is kind of an unusual syntax for Svelte but I think it's fine. I guess making a syntax inline with get and set required should (my assumption) work with the compiler |
It's weird (unnecessarily verbose) but also misleading, since it looks like you should be able to hoist the object |
@Rich-Harris this is great, but I seem to be getting some IDE errors, perhaps this stuff hasn't been added to the latest svelte language tools or other packages that the IDE depends on? |
the eslint plugin is not up to date yet, the other tools should be (assuming you bump to their latest version) |
I suggest adding demo from "Advent of Svelte" blog post to documentation itself. I believe it makes things obvious for the reader. I had to look up the demo few times myself :) |
WIP
closes #3937