Skip to content

Commit 99c20c4

Browse files
committed
Updates from feedback
1 parent fbdc328 commit 99c20c4

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/content/reference/react/useOptimistic.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export default function App() {
202202

203203
function onSubmit(newName) {
204204
startTransition(() => {
205-
setName(newName)
205+
setName(newName);
206206
});
207207
}
208208
return <EditName name={name} onSubmit={onSubmit} />;
@@ -457,7 +457,7 @@ Here, `liked => !liked` always toggles the latest state. If the base `isLiked` c
457457
458458
`useOptimistic` supports two patterns for calculating state based on current state:
459459
460-
**Updater functions** work just like [useState updaters](/reference/react/useState#updating-state-based-on-the-previous-state). Pass a function to the setter:
460+
**Updater functions** work like [useState updaters](/reference/react/useState#updating-state-based-on-the-previous-state). Pass a function to the setter:
461461
462462
```js
463463
const [optimistic, setOptimistic] = useOptimistic(value);
@@ -473,7 +473,7 @@ const [optimistic, dispatch] = useOptimistic(value, (current, action) => {
473473
dispatch(action);
474474
```
475475
476-
**Use updaters** for simple calculations where the setter call naturally describes the update. This is similar to using `setState(prev => ...)` with `useState`.
476+
**Use updaters** for calculations where the setter call naturally describes the update. This is similar to using `setState(prev => ...)` with `useState`.
477477
478478
**Use reducers** when you need to pass data to the update (like which item to add) or when handling multiple types of updates with a single hook.
479479
@@ -756,16 +756,23 @@ export default function ShoppingCart({ cart, cartActions }) {
756756
});
757757
}
758758

759-
const total = optimisticCart.reduce((sum, item) => sum + item.price * item.quantity, 0);
759+
const total = optimisticCart.reduce(
760+
(sum, item) => sum + item.price * item.quantity,
761+
0
762+
);
760763

761764
return (
762765
<div>
763766
<h2>Shopping Cart</h2>
764767
<div style={{ marginBottom: 16 }}>
765-
<button onClick={() => handleAdd({ id: 1, name: 'T-Shirt', price: 25 })}>
768+
<button onClick={() => handleAdd({
769+
id: 1, name: 'T-Shirt', price: 25
770+
})}>
766771
Add T-Shirt ($25)
767772
</button>{' '}
768-
<button onClick={() => handleAdd({ id: 2, name: 'Mug', price: 15 })}>
773+
<button onClick={() => handleAdd({
774+
id: 2, name: 'Mug', price: 15
775+
})}>
769776
Add Mug ($15)
770777
</button>
771778
</div>
@@ -778,7 +785,10 @@ export default function ShoppingCart({ cart, cartActions }) {
778785
{item.name} - ${item.price} ×
779786
{item.quantity}
780787
{' '}= ${item.price * item.quantity}
781-
<button onClick={() => handleRemove(item.id)} style={{ marginLeft: 8 }}>
788+
<button
789+
onClick={() => handleRemove(item.id)}
790+
style={{ marginLeft: 8 }}
791+
>
782792
Remove
783793
</button>
784794
{item.pending && ' ...'}
@@ -932,7 +942,7 @@ You may see this error:
932942
933943
<ConsoleLogLine level="error">
934944
935-
An optimistic state update occurred outside a transition or Action. To fix, move the update to an Action, or wrap with `startTransition`.
945+
An optimistic state update occurred outside a Transition or Action. To fix, move the update to an Action, or wrap with `startTransition`.
936946
937947
</ConsoleLogLine>
938948
@@ -941,7 +951,7 @@ An optimistic state update occurred outside a transition or Action. To fix, move
941951
The optimistic setter function must be called inside a Transition:
942952
943953
```js
944-
// Incorrect: outside a Transition
954+
// 🚩 Incorrect: outside a Transition
945955
function handleClick() {
946956
setOptimistic(newValue); // Warning!
947957
// ...
@@ -981,7 +991,7 @@ Cannot update optimistic state while rendering.
981991
This error occurs when you call the optimistic setter during the render phase of a component. You can only call it from event handlers, effects, or other callbacks:
982992
983993
```js
984-
// Incorrect: calling during render
994+
// 🚩 Incorrect: calling during render
985995
function MyComponent({ items }) {
986996
const [isPending, setPending] = useOptimistic(false);
987997

@@ -1005,7 +1015,7 @@ function MyComponent({ items }) {
10051015
// ...
10061016
}
10071017

1008-
// ✅ Also Correct: calling from an Action prop
1018+
// ✅ Also correct: calling from an Action prop
10091019
function MyComponent({ items }) {
10101020
const [isPending, setPending] = useOptimistic(false);
10111021

0 commit comments

Comments
 (0)