Skip to content

Commit e966858

Browse files
Addresses Issue #1183 (#1252)
Co-authored-by: Sarah <[email protected]>
1 parent 79af1ae commit e966858

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/routes/concepts/stores.mdx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,10 @@ Instead of relying on discovering individual indices, path syntax introduces sev
253253

254254
### Appending new values
255255

256-
To append new values to an array in a store, use the setter function with the spread operator (`...`) to create a new array that includes the existing items and the new ones.
257-
For appending a single element, you can instead leverage the "path syntax" by specifying the array’s length as the index to set.
256+
To append values to an array in a store, use the setter function with the spread operator (`...`) or the path syntax. Both methods add an element to the array but differ in how they modify it and their reactivity behavior.
257+
258+
The spread operator creates a new array by copying the existing elements and adding the new one, effectively replacing the entire `store.users` array.
259+
This replacement triggers reactivity for all effects that depend on the array or its properties.
258260

259261
```jsx
260262
setStore("users", (otherUsers) => [
@@ -266,9 +268,12 @@ setStore("users", (otherUsers) => [
266268
loggedIn: false,
267269
},
268270
])
271+
```
269272

270-
// can become
273+
The path syntax adds the new element by assigning it to the index equal to `store.users.length`, directly modifying the existing array.
274+
This triggers reactivity only for effects that depend on the new index or properties like `store.users.length`, making updates more efficient and targeted.
271275

276+
```jsx
272277
setStore("users", store.users.length, {
273278
id: 3,
274279
username: "michael584",

0 commit comments

Comments
 (0)