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
+184Lines changed: 184 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1207,6 +1207,190 @@ For example, if the Action performs a mutation (like writing to a database), abo
1207
1207
1208
1208
</Pitfall>
1209
1209
1210
+
---
1211
+
1212
+
### Handling errors {/*handling-errors*/}
1213
+
1214
+
There are two ways to handle errors with `useActionState`.
1215
+
1216
+
For known errors, such as "quantity not available" validation errors from your backend, you can return it as part of your `reducerAction` state and display it in the UI.
1217
+
1218
+
For unknown errors, such as `undefined is not a function`, you can throw an error. React will cancel all queued Actions and shows the nearest [Error Boundary](/reference/react/Component#catching-rendering-errors-with-an-error-boundary) by rethrowing the error from the `useActionState` hook.
1219
+
1220
+
<Sandpack>
1221
+
1222
+
```js src/App.js
1223
+
import {useActionState, startTransition} from 'react';
In this example, "Add 10" simulates an API that returns a validation error, which `updateCartAction` stores in state and displays inline. "Add NaN" results in an invalid count, so `updateCartAction` throws, which propagates through `useActionState` to the `ErrorBoundary` and shows a reset UI.
0 commit comments