@@ -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,20 @@ export async function updateQuantity(newQuantity) {
585585
586586你可以通过组件暴露一个 ` action ` 属性,允许父组件调用一个 Action。
587587
588- 例如,这个 ` TabButton ` 组件将其点击事件逻辑封装到 ` action ` 属性中:
588+ 例如,这个 ` TabButton ` 组件将 ` onClick ` 事件逻辑封装到 ` action ` 属性中:
589589
590- ``` js {8-10 }
590+ ``` js {8-12 }
591591export default function TabButton ({ action, children, isActive }) {
592592 const [isPending , startTransition ] = useTransition ();
593593 if (isActive) {
594594 return < b> {children}< / b>
595595 }
596596 return (
597597 < button onClick= {() => {
598- startTransition (() => {
599- action ();
598+ startTransition (async () => {
599+ // await the action that's passed in.
600+ // This allows it to be either sync or async.
601+ await action ();
600602 });
601603 }}>
602604 {children}
@@ -655,10 +657,15 @@ export default function TabButton({ action, children, isActive }) {
655657 if (isActive) {
656658 return < b> {children}< / b>
657659 }
660+ if (isPending) {
661+ return < b className= " pending" > {children}< / b> ;
662+ }
658663 return (
659- < button onClick= {() => {
660- startTransition (() => {
661- action ();
664+ < button onClick= {async () => {
665+ startTransition (async () => {
666+ // await the action that's passed in.
667+ // This allows it to be either sync or async.
668+ await action ();
662669 });
663670 }}>
664671 {children}
@@ -728,10 +735,19 @@ export default function ContactTab() {
728735``` css
729736button { margin-right : 10px }
730737b { display : inline-block ; margin-right : 10px ; }
738+ .pending { color : #777 ; }
731739```
732740
733741</Sandpack >
734742
743+ <Note >
744+
745+ When exposing an ` action ` prop from a component, you should ` await ` it inside the transition.
746+
747+ This allows the ` action ` callback to be either synchronous or asynchronous without requiring an additional ` startTransition ` to wrap the ` await ` in the action.
748+
749+ </Note >
750+
735751---
736752
737753### 显示待处理的视觉状态 {/* displaying-a-pending-visual-state* /}
@@ -803,8 +819,8 @@ export default function TabButton({ action, children, isActive }) {
803819 }
804820 return (
805821 < button onClick= {() => {
806- startTransition (() => {
807- action ();
822+ startTransition (async () => {
823+ await action ();
808824 });
809825 }}>
810826 {children}
@@ -1094,8 +1110,8 @@ export default function TabButton({ action, children, isActive }) {
10941110 }
10951111 return (
10961112 < button onClick= {() => {
1097- startTransition (() => {
1098- action ();
1113+ startTransition (async () => {
1114+ await action ();
10991115 });
11001116 }}>
11011117 {children}
@@ -1822,8 +1838,8 @@ import {startTransition} from 'react';
18221838export default function Item ({action}) {
18231839 function handleChange (e ) {
18241840 // Update the quantity in an Action.
1825- startTransition (() => {
1826- action (e .target .value );
1841+ startTransition (async () => {
1842+ await action (e .target .value );
18271843 });
18281844 }
18291845 return (
0 commit comments