You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react/useOptimistic.md
+93-78Lines changed: 93 additions & 78 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -103,15 +103,16 @@ If the Action throws an error, the Transition still ends, and React renders with
103
103
104
104
### `set` functions, like `setOptimistic(optimisticValue)` {/*setoptimistic*/}
105
105
106
-
The `set` function returned by `useOptimistic` lets you update the state to a different value inside an Action or Transition and trigger an immediate re-render. You can pass the next state directly, or a function that calculates it from the previous state:
106
+
The `set` function returned by `useOptimistic` lets you update the state for the duration of a Transition. You can pass the next state directly, or a function that calculates it from the previous state:
* The `set` function must be called inside a [Transition](/reference/react/useTransition) using [`startTransition`](/reference/react/startTransition) or inside an [Action](/reference/react/useTransition#perform-non-blocking-updates-with-actions). If you call the setter outside an Action or Transition, the optimistic value will briefly appear and then immediately revert.
132
+
* The `set` function must be called inside a [Transition](/reference/react/useTransition) using [`startTransition`](/reference/react/startTransition) or inside an [Action](/reference/react/useTransition#perform-non-blocking-updates-with-actions). If you call the setter outside an Action or Transition, [React will show a warning](#an-optimistic-state-update-occurred-outside-a-transition-or-action) and the optimistic value will briefly render.
132
133
133
134
---
134
135
135
136
## Usage {/*usage*/}
136
137
137
138
### Adding optimistic state to a component {/*adding-optimistic-state-to-a-component*/}
138
139
139
-
Call useOptimistic at the top level of your component to declare one or more optimistic states.
140
+
Call `useOptimistic` at the top level of your component to declare one or more optimistic states.
@@ -152,7 +153,7 @@ function MyComponent({age, name, todos}) {
152
153
153
154
1. The <CodeStep step={2}>current state</CodeStep> of the optimistic value, returning the <CodeStep step={1}>state</CodeStep> provided.
154
155
2. The <CodeStep step={3}>set function</CodeStep> that lets you temporarily change the state during an Action or Transition.
155
-
* If a <CodeStep step={1}>reducer</CodeStep> is provided, it will run before rendering the temporary state.
156
+
* If a <CodeStep step={4}>reducer</CodeStep> is provided, it will run before rendering the temporary state.
156
157
157
158
To use the optimistic state, call the set function inside a Transition:
158
159
@@ -165,17 +166,99 @@ function handleClick(e) {
165
166
}
166
167
```
167
168
168
-
Or call it inside an action:
169
+
React will render the optimistic state first, then complete the Transition with the new value.
169
170
170
-
```js [[3, 3, "setOptimisticName"]]
171
+
<Note>
172
+
173
+
When using [Action props](/reference/react/useTransition#exposing-action-props-from-components), you can call the set function without `startTransition`:
174
+
175
+
```js [[3, 2, "setOptimisticName"]]
171
176
asyncfunctionsubmitAction() {
172
-
// By convention, Actions are called inside startTransition.
173
177
setOptimisticName('Taylor');
174
178
awaitupdateName('Taylor');
175
179
}
176
180
```
177
181
178
-
React will render the optimistic state first, then complete the Action or Transition with the new value.
182
+
This works because Action props are already called inside a Transition.
183
+
184
+
For an example, see: [Using optimistic state in Action props](#using-optimistic-state-in-action-props).
185
+
186
+
</Note>
187
+
188
+
### Using optimistic state in Action props {/*using-optimistic-state-in-action-props*/}
189
+
190
+
In an [Action prop](/reference/react/useTransition#exposing-action-props-from-components), you can call the optimistic setter directly without `startTransition`.
191
+
192
+
This example sets optimistc state inside a `<form>``submitAction` prop:
In this example, when the user submits the form, the `optimisticName` updates immediately to show the new value while the server request is in progress. When the request completes, the real `name` is updated and the Transition completes rendering the new name.
252
+
253
+
<Note>
254
+
255
+
#### Why doesn't this need `startTransition`? {/*why-doesnt-this-need-starttransition*/}
256
+
257
+
The optimistic setter must be called inside a Transition to work correctly. When you pass a function to an [Action prop](/reference/react/useTransition#exposing-action-props-from-components), by convention that function is already called inside `startTransition`.
258
+
259
+
Since you're already in a Transition, calling `setOptimisticName` directly is valid.
260
+
261
+
</Note>
179
262
180
263
### Adding pending state to a component {/*adding-pending-state-to-a-component*/}
181
264
@@ -488,74 +571,6 @@ export async function unfollowUser(name) {
488
571
489
572
The reducer receives the new `isFollowing` value and calculates both the new follow state and the updated follower count in a single update. This ensures the button text and count always stay in sync.
490
573
491
-
### Using optimistic state in Action props {/*using-optimistic-state-in-action-props*/}
492
-
493
-
When you pass a function to an [Action prop](/reference/react/useTransition#exposing-action-props-from-components), you can call the optimistic setter directly without wrapping it in `startTransition`. This includes form actions:
In this example, when the user submits the form, the `optimisticName` updates immediately to show the new value while the server request is in progress. When the request completes, React updates the real `name` state and the Transition ends.
548
-
549
-
<DeepDive>
550
-
551
-
#### Why doesn't this need `startTransition`? {/*why-doesnt-this-need-starttransition*/}
552
-
553
-
The optimistic setter must be called inside a Transition to work correctly. When you pass a function to an Action prop, React automatically calls that function inside a Transition context. This means the action is already running inside a Transition when invoked.
554
-
555
-
Since you're already in a Transition, calling `setOptimisticName` directly is valid. The `await` inside the async function keeps the Transition open until the server responds, at which point the optimistic state reverts to the real state.
556
-
557
-
</DeepDive>
558
-
559
574
### Optimistically adding to a list {/*optimistically-adding-to-a-list*/}
560
575
561
576
When you need to optimistically add items to a list, use the `reducer` parameter:
0 commit comments