@@ -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
228228export 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 }
591595export 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
729740button { margin-right : 10px }
730741b { 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';
18221842export 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