Skip to content

Commit ce4aa0d

Browse files
committed
docs(en): merging all conflicts
2 parents 69a6525 + a3e9466 commit ce4aa0d

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

src/content/reference/react/useOptimistic.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ function Thread({ messages, sendMessageAction }) {
7474
function formAction(formData) {
7575
addOptimisticMessage(formData.get("message"));
7676
formRef.current.reset();
77-
sendMessageAction(formData);
77+
startTransition(async () => {
78+
await sendMessageAction(formData);
79+
});
7880
}
7981
const [optimisticMessages, addOptimisticMessage] = useOptimistic(
8082
messages,
@@ -108,12 +110,10 @@ export default function App() {
108110
const [messages, setMessages] = useState([
109111
{ text: "你好,在这儿!", sending: false, key: 1 }
110112
]);
111-
function sendMessageAction(formData) {
112-
startTransition(async () => {
113-
const sentMessage = await deliverMessage(formData.get("message"));
114-
startTransition(() => {
115-
setMessages((messages) => [{ text: sentMessage }, ...messages]);
116-
})
113+
async function sendMessageAction(formData) {
114+
const sentMessage = await deliverMessage(formData.get("message"));
115+
startTransition(() => {
116+
setMessages((messages) => [{ text: sentMessage }, ...messages]);
117117
})
118118
}
119119
return <Thread messages={messages} sendMessageAction={sendMessageAction} />;

src/content/reference/react/useTransition.md

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ function SubmitButton({ submitAction }) {
7777
<button
7878
disabled={isPending}
7979
onClick={() => {
80-
startTransition(() => {
81-
submitAction();
80+
startTransition(async () => {
81+
await submitAction();
8282
});
8383
}}
8484
>
@@ -227,9 +227,9 @@ import { startTransition } from "react";
227227

228228
export default function Item({action}) {
229229
function handleChange(event) {
230-
// To expose an action prop, call the callback in startTransition.
230+
// To expose an action prop, await the callback in startTransition.
231231
startTransition(async () => {
232-
action(event.target.value);
232+
await action(event.target.value);
233233
})
234234
}
235235
return (
@@ -585,18 +585,24 @@ export async function updateQuantity(newQuantity) {
585585

586586
你可以通过组件暴露一个 `action` 属性,允许父组件调用一个 Action。
587587

588+
<<<<<<< HEAD
588589
例如,这个 `TabButton` 组件将其点击事件逻辑封装到 `action` 属性中:
590+
=======
591+
For example, this `TabButton` component wraps its `onClick` logic in an `action` prop:
592+
>>>>>>> a3e9466dfeea700696211533a3570bc48d7bc3d3
589593
590-
```js {8-10}
594+
```js {8-12}
591595
export default function TabButton({ action, children, isActive }) {
592596
const [isPending, startTransition] = useTransition();
593597
if (isActive) {
594598
return <b>{children}</b>
595599
}
596600
return (
597601
<button onClick={() => {
598-
startTransition(() => {
599-
action();
602+
startTransition(async () => {
603+
// await the action that's passed in.
604+
// This allows it to be either sync or async.
605+
await action();
600606
});
601607
}}>
602608
{children}
@@ -655,10 +661,15 @@ export default function TabButton({ action, children, isActive }) {
655661
if (isActive) {
656662
return <b>{children}</b>
657663
}
664+
if (isPending) {
665+
return <b className="pending">{children}</b>;
666+
}
658667
return (
659-
<button onClick={() => {
660-
startTransition(() => {
661-
action();
668+
<button onClick={async () => {
669+
startTransition(async () => {
670+
// await the action that's passed in.
671+
// This allows it to be either sync or async.
672+
await action();
662673
});
663674
}}>
664675
{children}
@@ -728,10 +739,19 @@ export default function ContactTab() {
728739
```css
729740
button { margin-right: 10px }
730741
b { display: inline-block; margin-right: 10px; }
742+
.pending { color: #777; }
731743
```
732744

733745
</Sandpack>
734746

747+
<Note>
748+
749+
When exposing an `action` prop from a component, you should `await` it inside the transition.
750+
751+
This allows the `action` callback to be either synchronous or asynchronous without requiring an additional `startTransition` to wrap the `await` in the action.
752+
753+
</Note>
754+
735755
---
736756

737757
### 显示待处理的视觉状态 {/*displaying-a-pending-visual-state*/}
@@ -803,8 +823,8 @@ export default function TabButton({ action, children, isActive }) {
803823
}
804824
return (
805825
<button onClick={() => {
806-
startTransition(() => {
807-
action();
826+
startTransition(async () => {
827+
await action();
808828
});
809829
}}>
810830
{children}
@@ -1094,8 +1114,8 @@ export default function TabButton({ action, children, isActive }) {
10941114
}
10951115
return (
10961116
<button onClick={() => {
1097-
startTransition(() => {
1098-
action();
1117+
startTransition(async () => {
1118+
await action();
10991119
});
11001120
}}>
11011121
{children}
@@ -1822,8 +1842,8 @@ import {startTransition} from 'react';
18221842
export default function Item({action}) {
18231843
function handleChange(e) {
18241844
// Update the quantity in an Action.
1825-
startTransition(() => {
1826-
action(e.target.value);
1845+
startTransition(async () => {
1846+
await action(e.target.value);
18271847
});
18281848
}
18291849
return (

0 commit comments

Comments
 (0)