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
+98-30Lines changed: 98 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1379,24 +1379,6 @@ In this example, "Add 10" simulates an API that returns a validation error, whic
1379
1379
1380
1380
## Troubleshooting {/*troubleshooting*/}
1381
1381
1382
-
### My Action can no longer read the submitted form data {/*action-cant-read-form-data*/}
1383
-
1384
-
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.
1385
-
1386
-
```js
1387
-
// Without useActionState
1388
-
functionaction(formData) {
1389
-
constname=formData.get('name');
1390
-
}
1391
-
1392
-
// With useActionState
1393
-
functionaction(prevState, formData) {
1394
-
constname=formData.get('name');
1395
-
}
1396
-
```
1397
-
1398
-
---
1399
-
1400
1382
### My `isPending` flag is not updating {/*ispending-not-updating*/}
1401
1383
1402
1384
If you're calling `dispatchAction` manually (not through an Action prop), make sure you wrap the call in [`startTransition`](/reference/react/startTransition):
@@ -1422,28 +1404,27 @@ When `dispatchAction` is passed to an Action prop, React automatically wraps it
1422
1404
1423
1405
---
1424
1406
1425
-
### I'm getting an error: "Cannot update form state while rendering" {/*cannot-update-during-render*/}
1407
+
### My Action cannot read form data {/*action-cannot-read-form-data*/}
1426
1408
1427
-
You cannot call `dispatchAction` during render. This causes an infinite loop because calling `dispatchAction` schedules a state update, which triggers a re-render, which calls `dispatchAction` again.
1409
+
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.
Only call `dispatchAction` in response to user events (like form submissions or button clicks) or in Effects.
1441
-
1442
1423
---
1443
1424
1444
1425
### My actions are being skipped {/*actions-skipped*/}
1445
1426
1446
-
If you call `dispatchAction` multiple times and some of them don't run, it may be because an earlier `dispatchAction` call threw an error.
1427
+
If you call `dispatchAction` multiple times and some of them don't run, it may be because an earlier `dispatchAction` call threw an error.
1447
1428
1448
1429
When a `reducerAction` throws, React skips all subsequently queued `dispatchAction` calls.
1449
1430
@@ -1493,4 +1474,91 @@ function MyComponent() {
1493
1474
}
1494
1475
```
1495
1476
1496
-
Alternatively, you can add a `key` prop to the component using `useActionState` to force it to remount with fresh state.
1477
+
Alternatively, you can add a `key` prop to the component using `useActionState` to force it to remount with fresh state, or a `<form>``action` prop, which resets automatically after submission.
1478
+
1479
+
---
1480
+
1481
+
### I'm getting an error: "An async function with useActionState was called outside of a transition." {/*async-function-outside-transition*/}
1482
+
1483
+
A common mistake is to forget to call `dispatchAction` from inside a Transition:
1484
+
1485
+
<ConsoleBlockMulti>
1486
+
<ConsoleLogLine level="error">
1487
+
1488
+
An async function with useActionState was called outside of a transition. This is likely not what you intended (for example, isPending will not update correctly). Either call the returned function inside startTransition, or pass it to an `action` or `formAction` prop.
1489
+
1490
+
</ConsoleLogLine>
1491
+
</ConsoleBlockMulti>
1492
+
1493
+
1494
+
This error happens because `dispatchAction` must run inside a Transition:
### I'm getting an error: "Cannot update action state while rendering" {/*cannot-update-during-render*/}
1542
+
1543
+
You cannot call `dispatchAction` during render:
1544
+
1545
+
<ConsoleBlock level="error">
1546
+
1547
+
Cannot update action state while rendering.
1548
+
1549
+
</ConsoleBlock>
1550
+
1551
+
This causes an infinite loop because calling `dispatchAction` schedules a state update, which triggers a re-render, which calls `dispatchAction` again.
0 commit comments