Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #271 from trusktr/patch-3
Browse files Browse the repository at this point in the history
Update createMutable doc to describe a gotcha
  • Loading branch information
davedbase authored Oct 9, 2023
2 parents c8f2bff + 45b641e commit 4f0fd2d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions langs/en/api/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,34 @@ const user = createMutable({
});
```
#### Gotchas
Note that expressions like `state.someValue++` are equivalent to `state.someValue = state.someValue + 1`, which is both a read and a write. If you read and write to the same signal in an effect, it will be an infinite loop until a maximum call stack size error happens:
```js
// Inifinite loop crash:
createEffect(() => {
state.someValue = state.someValue + 1
})

// Inifinite loop crash:
createEffect(() => {
state.someValue++
})
```
The solution for these cases is using `untrack`:
```js
createEffect(() => {
state.someValue = untrack(() => state.someValue + 1)
})

createEffect(() => {
untrack(() => state.someValue++)
})
```
### `modifyMutable`
**New in v1.4.0**
Expand Down

0 comments on commit 4f0fd2d

Please sign in to comment.