Skip to content

Commit 3cd37ac

Browse files
committed
document callback
1 parent 40fdcdf commit 3cd37ac

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

documentation/docs/03-runes/01-state.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ console.log(a, b); // 3, 3
8989

9090
As with `$state`, if `$state.link` is passed a plain object or array it will be made deeply reactive. If passed an existing state proxy it will be reused, meaning that mutating the linked state will mutate the original. To clone a state proxy, you can use [`$state.snapshot`](#$state-snapshot).
9191

92+
If you pass a callback to `$state.link`, changes to the input value will invoke the callback rather than updating the linked state, allowing you to choose whether to (for example) preserve or discard local changes, or merge incoming changes with local ones:
93+
94+
```js
95+
let { stuff } = $props();
96+
97+
let incoming = $state();
98+
let hasUnsavedChanges = $state(false);
99+
100+
let current = $state.link({ ...stuff }, (stuff) => {
101+
if (hasUnsavedChanges) {
102+
incoming = stuff;
103+
} else {
104+
incoming = null;
105+
current = stuff;
106+
}
107+
});
108+
```
109+
92110
## `$state.raw`
93111

94112
State declared with `$state.raw` cannot be mutated; it can only be _reassigned_. In other words, rather than assigning to a property of an object, or using an array method like `push`, replace the object or array altogether if you'd like to update it:

sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ console.log(a, b); // 3, 3
9191

9292
As with `$state`, if `$state.link` is passed a plain object or array it will be made deeply reactive. If passed an existing state proxy it will be reused, meaning that mutating the linked state will mutate the original. To clone a state proxy, you can use [`$state.snapshot`](#$state-snapshot).
9393

94+
If you pass a callback to `$state.link`, changes to the input value will invoke the callback rather than updating the linked state, allowing you to choose whether to (for example) preserve or discard local changes, or merge incoming changes with local ones:
95+
96+
```js
97+
let { stuff } = $props();
98+
99+
let incoming = $state();
100+
let hasUnsavedChanges = $state(false);
101+
102+
let current = $state.link({ ...stuff }, (stuff) => {
103+
if (hasUnsavedChanges) {
104+
incoming = stuff;
105+
} else {
106+
incoming = null;
107+
current = stuff;
108+
}
109+
});
110+
```
111+
94112
## `$state.raw`
95113

96114
State declared with `$state.raw` cannot be mutated; it can only be _reassigned_. In other words, rather than assigning to a property of an object, or using an array method like `push`, replace the object or array altogether if you'd like to update it:

0 commit comments

Comments
 (0)