Skip to content

Commit 83c71c4

Browse files
pubkeyclaude
andauthored
docs: show how to update an array in the RxState (#9762)
Claude-Session: https://claude.ai/code/session_015P6w6hnnyVPXwotts6YQ2F Co-authored-by: Claude <noreply@anthropic.com>
1 parent 87606bf commit 83c71c4

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

‎docs-src/docs/rx-state.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ await myState.set('myField', v => v + 1);
6060
await myState.set('myField', v => 42);
6161
```
6262

63+
To change an item of an array, use a modifier on the array itself. The modifier gets the plain array, changes it with normal JavaScript, and returns it:
64+
65+
```ts
66+
await myState.set('myArray', myArray => {
67+
myArray[3] = 'foo';
68+
return myArray;
69+
});
70+
```
71+
6372
When the modifier returns `undefined`, the value at that path is removed from the state. This works the same with and without a [schema validator](./schema-validation.md).
6473

6574
The modifier is used instead of a direct assignment to ensure correct behavior when other JavaScript realms write to the state at the same time, like other browser tabs or WebWorkers. On conflicts, the modifier will just be run again to ensure deterministic and correct behavior. Therefore mutation is `async`, you have to `await` the call to the set function when you care about the moment when the change actually happened.

0 commit comments

Comments
 (0)