Skip to content

Commit d83be47

Browse files
committed
Edit down a bit
1 parent 0f71d9d commit d83be47

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

src/content/reference/react/useOptimistic.md

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,7 @@ See [Updating state based on the current state](#updating-state-based-on-current
388388
389389
### Updating state based on the current state {/*updating-state-based-on-current-state*/}
390390
391-
The previous example toggles `isLiked` by reading from the current optimistic state:
392-
393-
```js
394-
setOptimisticIsLiked(!optimisticIsLiked);
395-
```
396-
397-
This works fine for simple cases, but can cause issues if the base state changes while your action is pending. For example, if another user's action changes the `isLiked` state on the server while you're waiting, your toggle could apply to the wrong value.
391+
The [previous example](#updating-props-or-state-optimistically) reads from the current optimistic state, which can cause issues if the base state changes while the action is pending.
398392
399393
To calculate the optimistic state relative to the current state, pass an updater function:
400394
@@ -473,13 +467,6 @@ const [optimistic, dispatch] = useOptimistic(value, (current, action) => {
473467
dispatch(action);
474468
```
475469
476-
| Use Case | Pattern | Example |
477-
|----------|---------|---------|
478-
| Boolean toggle | Updater | `setOptimistic(liked => !liked)` |
479-
| Increment/decrement | Updater | `setOptimistic(count => count + 1)` |
480-
| Add item to list | Reducer | `useOptimistic(items, (list, item) => [...list, item])` |
481-
| Multiple action types | Reducer | `useOptimistic(state, (current, action) => { switch(action.type) { ... } })` |
482-
483470
**Use updaters** for simple calculations where the setter call naturally describes the update. This is similar to using `setState(prev => ...)` with `useState`.
484471
485472
**Use reducers** when you need to pass data to the update (like which item to add) or when handling multiple types of updates with a single hook.
@@ -1019,11 +1006,7 @@ function MyComponent({ items }) {
10191006
10201007
### My optimistic updates show stale values {/*my-optimistic-updates-show-stale-values*/}
10211008
1022-
If your optimistic state seems to be based on old data, it might be because the canonical state changed while your action was pending. React will update your optimistic value by re-running `reducer` with the new state.
1023-
1024-
If you're using the simple form without `reducer`, the value you passed directly replaces the state, which may not be what you want when the base state has changed. Consider using the `reducer` form to calculate the optimistic state relative to the current state.
1025-
1026-
See [Updating state based on the current state](#updating-state-based-on-current-state) for a detailed explanation.
1009+
If your optimistic state seems to be based on old data, consider using an updater function or reducer to calculate the optimistic state relative to the current state.
10271010
10281011
```js
10291012
// May show stale data if state changes during action
@@ -1035,6 +1018,8 @@ const [optimistic, adjust] = useOptimistic(count, (current, delta) => current +
10351018
adjust(1); // Always adds 1 to whatever the current count is
10361019
```
10371020
1021+
See [Updating state based on the current state](#updating-state-based-on-current-state) for details.
1022+
10381023
### I don't know if my optimistic update is pending {/*i-dont-know-if-my-optimistic-update-is-pending*/}
10391024
10401025
To know when `useOptimistic` is pending, you have two options:

0 commit comments

Comments
 (0)