@@ -77,8 +77,8 @@ function SubmitButton({ submitAction }) {
77
77
< button
78
78
disabled= {isPending}
79
79
onClick= {() => {
80
- startTransition (() => {
81
- submitAction ();
80
+ startTransition (async () => {
81
+ await submitAction ();
82
82
});
83
83
}}
84
84
>
@@ -227,9 +227,9 @@ import { startTransition } from "react";
227
227
228
228
export default function Item ({action}) {
229
229
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.
231
231
startTransition (async () => {
232
- action (event .target .value );
232
+ await action (event .target .value );
233
233
})
234
234
}
235
235
return (
@@ -585,18 +585,20 @@ export async function updateQuantity(newQuantity) {
585
585
586
586
你可以通过组件暴露一个 ` action ` 属性,允许父组件调用一个 Action。
587
587
588
- 例如,这个 ` TabButton ` 组件将其点击事件逻辑封装到 ` action ` 属性中:
588
+ 例如,这个 ` TabButton ` 组件将 ` onClick ` 事件逻辑封装到 ` action ` 属性中:
589
589
590
- ``` js {8-10 }
590
+ ``` js {8-12 }
591
591
export default function TabButton ({ action, children, isActive }) {
592
592
const [isPending , startTransition ] = useTransition ();
593
593
if (isActive) {
594
594
return < b> {children}< / b>
595
595
}
596
596
return (
597
597
< 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 ();
600
602
});
601
603
}}>
602
604
{children}
@@ -655,10 +657,15 @@ export default function TabButton({ action, children, isActive }) {
655
657
if (isActive) {
656
658
return < b> {children}< / b>
657
659
}
660
+ if (isPending) {
661
+ return < b className= " pending" > {children}< / b> ;
662
+ }
658
663
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 ();
662
669
});
663
670
}}>
664
671
{children}
@@ -728,10 +735,19 @@ export default function ContactTab() {
728
735
``` css
729
736
button { margin-right : 10px }
730
737
b { display : inline-block ; margin-right : 10px ; }
738
+ .pending { color : #777 ; }
731
739
```
732
740
733
741
</Sandpack >
734
742
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
+
735
751
---
736
752
737
753
### 显示待处理的视觉状态 {/* displaying-a-pending-visual-state* /}
@@ -803,8 +819,8 @@ export default function TabButton({ action, children, isActive }) {
803
819
}
804
820
return (
805
821
< button onClick= {() => {
806
- startTransition (() => {
807
- action ();
822
+ startTransition (async () => {
823
+ await action ();
808
824
});
809
825
}}>
810
826
{children}
@@ -1094,8 +1110,8 @@ export default function TabButton({ action, children, isActive }) {
1094
1110
}
1095
1111
return (
1096
1112
< button onClick= {() => {
1097
- startTransition (() => {
1098
- action ();
1113
+ startTransition (async () => {
1114
+ await action ();
1099
1115
});
1100
1116
}}>
1101
1117
{children}
@@ -1822,8 +1838,8 @@ import {startTransition} from 'react';
1822
1838
export default function Item ({action}) {
1823
1839
function handleChange (e ) {
1824
1840
// Update the quantity in an Action.
1825
- startTransition (() => {
1826
- action (e .target .value );
1841
+ startTransition (async () => {
1842
+ await action (e .target .value );
1827
1843
});
1828
1844
}
1829
1845
return (
0 commit comments