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/useActionState.md
+10-9Lines changed: 10 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,7 +56,7 @@ function MyCart({initialState}) {
56
56
#### Caveats {/*caveats*/}
57
57
58
58
* `useActionState` is a Hook, so you can only call it **at the top level of your component** or your own Hooks. You can't call it inside loops or conditions. If you need that, extract a new component and move the state into it.
59
-
* React queues and executes multiple calls to `dispatchAction` sequentially, allowing each `reducerAction`to use the result of the previous Action.
59
+
* React queues and executes multiple calls to `dispatchAction` sequentially. Each call to `reducerAction`receives the result of the previous call.
60
60
* The `dispatchAction` function has a stable identity, so you will often see it omitted from Effect dependencies, but including it will not cause the Effect to fire. If the linter lets you omit a dependency without errors, it is safe to do. [Learn more about removing Effect dependencies.](/learn/removing-effect-dependencies#move-dynamic-objects-and-functions-inside-your-effect)
61
61
* When using the `permalink` option, ensure the same form component is rendered on the destination page (including the same `reducerAction` and `permalink`) so React knows how to pass the state through. Once the page becomes interactive, this parameter has no effect.
62
62
* When using Server Functions, `initialState` needs to be [serializable](/reference/rsc/use-server#serializable-parameters-and-return-values) (values like plain objects, arrays, strings, and numbers).
@@ -65,9 +65,9 @@ function MyCart({initialState}) {
65
65
66
66
<Note>
67
67
68
-
`dispatchAction` must be called within a Transition.
68
+
`dispatchAction` must be called from an Action.
69
69
70
-
You can wrap it in [`startTransition`](/reference/react/startTransition), or pass it to an [Action prop](/reference/react/useTransition#exposing-action-props-from-components).
70
+
You can wrap it in [`startTransition`](/reference/react/startTransition), or pass it to an [Action prop](/reference/react/useTransition#exposing-action-props-from-components). Calls outside that scope won’t be treated as part of the Transition and [log an error](#async-function-outside-transition) on development mode.
71
71
72
72
</Note>
73
73
@@ -86,11 +86,11 @@ async function reducerAction(previousState, actionPayload) {
86
86
}
87
87
```
88
88
89
-
Each time you call `dispatchAction`, React calls the `reducerAction` with the `actionPayload`. The reducer will perform side effects such as posting data, and return the new state. If `dispatchAction` is called multiple times, React queues and executes them in order so the result of the previous call is available for the current call.
89
+
Each time you call `dispatchAction`, React calls the `reducerAction` with the `actionPayload`. The reducer will perform side effects such as posting data, and return the new state. If `dispatchAction` is called multiple times, React queues and executes them in order so the result of the previous call is passed as `previousState` for the current call.
90
90
91
91
#### Parameters {/*reduceraction-parameters*/}
92
92
93
-
* `previousState`: The current state. Initially this is equal to the `initialState`. After the first call to `dispatchAction`, it's equal to the last state returned.
93
+
* `previousState`: The last state. Initially this is equal to the `initialState`. After the first call to `dispatchAction`, it's equal to the last state returned.
94
94
95
95
* **optional** `actionPayload`: The argument passed to `dispatchAction`. It can be a value of any type. Similar to `useReducer` conventions, it is usually an object with a `type` property identifying it and, optionally, other properties with additional information.
96
96
@@ -104,6 +104,7 @@ Each time you call `dispatchAction`, React calls the `reducerAction` with the `a
104
104
* `reducerAction` is not invoked twice in `<StrictMode>` since `reducerAction` is designed to allow side effects.
105
105
* The return type of `reducerAction` must match the type of `initialState`. If TypeScript infers a mismatch, you may need to explicitly annotate your state type.
106
106
* If you set state after `await` in the `reducerAction` you currently need to wrap the state update in an additional `startTransition`. See the [startTransition](/reference/react/useTransition#react-doesnt-treat-my-state-update-after-await-as-a-transition) docs for more info.
107
+
* When using Server Functions, `actionPayload` needs to be [serializable](/reference/rsc/use-server#serializable-parameters-and-return-values) (values like plain objects, arrays, strings, and numbers).
107
108
108
109
<DeepDive>
109
110
@@ -611,7 +612,7 @@ hr {
611
612
612
613
### Using with Action props {/*using-with-action-props*/}
613
614
614
-
When you pass the `dispatchAction` function to a component that exposes an [Action prop](/reference/react/useTransition#exposing-action-props-from-components), you don't need to call `startTransition` or `useOptmisitc` yourself.
615
+
When you pass the `dispatchAction` function to a component that exposes an [Action prop](/reference/react/useTransition#exposing-action-props-from-components), you don't need to call `startTransition` or `useOptimistic` yourself.
615
616
616
617
This example shows using the `increaseAction` and `decreaseAction` props of a QuantityStepper component:
617
618
@@ -679,7 +680,7 @@ export default function QuantityStepper({value, increaseAction, decreaseAction})
679
680
680
681
functionhandleDecrease() {
681
682
startTransition(async () => {
682
-
setOptimisticValue(c=>c +1);
683
+
setOptimisticValue(c=>Math.max(0, c -1));
683
684
awaitdecreaseAction();
684
685
});
685
686
}
@@ -879,7 +880,7 @@ export default function QuantityStepper({value, increaseAction, decreaseAction})
879
880
880
881
functionhandleDecrease() {
881
882
startTransition(async () => {
882
-
setOptimisticValue(c=>c +1);
883
+
setOptimisticValue(c=>Math.max(0, c -1));
883
884
awaitdecreaseAction();
884
885
});
885
886
}
@@ -1408,7 +1409,7 @@ When `dispatchAction` is passed to an Action prop, React automatically wraps it
1408
1409
1409
1410
When you use `useActionState`, the `reducerAction` receives an extra argument as its first argument: the previous or initial state. The submitted form data is therefore its second argument instead of its first.
0 commit comments