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
When you click to increase or decrease the quantity, an `"ADD"` or `"REMOVE"` is dispatched. In the `reducerAction`, different APIs are called to update the quantity.
431
+
432
+
In this example, we use the pending state of the Actions to replace both the quantity and the total. If you want to provide immediate feedback, such as immediately updating the quantity, you can use `useOptimistic`.
433
+
426
434
<DeepDive>
427
435
428
436
#### How is `useActionState` different from `useReducer`? {/*useactionstate-vs-usereducer*/}
@@ -439,44 +447,52 @@ You can think of `useActionState` as `useReducer` for side effects from user Act
439
447
440
448
---
441
449
442
-
### Using with Action props {/*using-with-action-props*/}
450
+
### Using with `useOptimistic`{/*using-with-useoptimistic*/}
443
451
444
-
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 wrap the call in `startTransition` yourself.
452
+
You can combine `useActionState` with [`useOptimistic`](/reference/react/useOptimistic) to show immediate UI feedback:
445
453
446
-
This example shows using the `increaseAction` and `decreaseAction` props of a QuantityStepper component:
Since `<QuantityStepper>` has built-in support for pending state, the loading indicator is shown automatically.
606
+
607
+
`setOptimisticCount` immediately updates the quantity, and `dispatchAction()` queues the `updateCartAction`. A pending indicator appears on both the quantity and total to give the user feedback that their update is still being applied.
622
608
623
609
---
624
610
625
-
### Using with `useOptimistic` {/*using-with-useoptimistic*/}
626
611
627
-
You can combine `useActionState` with [`useOptimistic`](/reference/react/useOptimistic) to show immediate UI feedback:
612
+
### Using with Action props {/*using-with-action-props*/}
628
613
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
+
616
+
This example shows using the `increaseAction` and `decreaseAction` props of a QuantityStepper component:
When the stepper arrow is clicked, `setOptimisticCount` immediately updates the quantity, and `dispatchAction()` queues the `updateCartAction`. A pending indicator appears on both the quantity and total to give the user feedback that their update is still being applied.
793
+
Since `<QuantityStepper>` has built-in support for transitions, pending state, and optimistically updating the count, you just need to tell the Action _what_ to change, and _how_ to change it is handled for you.
0 commit comments