Skip to content

Commit 7544033

Browse files
committed
Feedback
1 parent 1b10f12 commit 7544033

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/content/reference/react/useActionState.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function MyCart({initialState}) {
5656
#### Caveats {/*caveats*/}
5757
5858
* `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.
6060
* 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)
6161
* 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.
6262
* 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}) {
6565
6666
<Note>
6767
68-
`dispatchAction` must be called within a Transition.
68+
`dispatchAction` must be called from an Action.
6969
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.
7171
7272
</Note>
7373
@@ -86,11 +86,11 @@ async function reducerAction(previousState, actionPayload) {
8686
}
8787
```
8888
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.
9090
9191
#### Parameters {/*reduceraction-parameters*/}
9292
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.
9494
9595
* **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.
9696
@@ -104,6 +104,7 @@ Each time you call `dispatchAction`, React calls the `reducerAction` with the `a
104104
* `reducerAction` is not invoked twice in `<StrictMode>` since `reducerAction` is designed to allow side effects.
105105
* 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.
106106
* 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).
107108
108109
<DeepDive>
109110
@@ -611,7 +612,7 @@ hr {
611612
612613
### Using with Action props {/*using-with-action-props*/}
613614
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.
615616
616617
This example shows using the `increaseAction` and `decreaseAction` props of a QuantityStepper component:
617618
@@ -679,7 +680,7 @@ export default function QuantityStepper({value, increaseAction, decreaseAction})
679680

680681
function handleDecrease() {
681682
startTransition(async () => {
682-
setOptimisticValue(c => c + 1);
683+
setOptimisticValue(c => Math.max(0, c - 1));
683684
await decreaseAction();
684685
});
685686
}
@@ -879,7 +880,7 @@ export default function QuantityStepper({value, increaseAction, decreaseAction})
879880

880881
function handleDecrease() {
881882
startTransition(async () => {
882-
setOptimisticValue(c => c + 1);
883+
setOptimisticValue(c => Math.max(0, c - 1));
883884
await decreaseAction();
884885
});
885886
}
@@ -1408,7 +1409,7 @@ When `dispatchAction` is passed to an Action prop, React automatically wraps it
14081409
14091410
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.
14101411
1411-
```js
1412+
```js {2,7}
14121413
// Without useActionState
14131414
function action(formData) {
14141415
const name = formData.get('name');

0 commit comments

Comments
 (0)