@@ -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,24 @@ export async function updateQuantity(newQuantity) {
585
585
586
586
你可以通过组件暴露一个 ` action ` 属性,允许父组件调用一个 Action。
587
587
588
+ <<<<<<< HEAD
588
589
例如,这个 ` TabButton ` 组件将其点击事件逻辑封装到 ` action ` 属性中:
590
+ =======
591
+ For example, this ` TabButton ` component wraps its ` onClick ` logic in an ` action ` prop:
592
+ >>>>>>> a3e9466dfeea700696211533a3570bc48d7bc3d3
589
593
590
- ``` js {8-10 }
594
+ ``` js {8-12 }
591
595
export default function TabButton ({ action, children, isActive }) {
592
596
const [isPending , startTransition ] = useTransition ();
593
597
if (isActive) {
594
598
return < b> {children}< / b>
595
599
}
596
600
return (
597
601
< 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 ();
600
606
});
601
607
}}>
602
608
{children}
@@ -655,10 +661,15 @@ export default function TabButton({ action, children, isActive }) {
655
661
if (isActive) {
656
662
return < b> {children}< / b>
657
663
}
664
+ if (isPending) {
665
+ return < b className= " pending" > {children}< / b> ;
666
+ }
658
667
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 ();
662
673
});
663
674
}}>
664
675
{children}
@@ -728,10 +739,19 @@ export default function ContactTab() {
728
739
``` css
729
740
button { margin-right : 10px }
730
741
b { display : inline-block ; margin-right : 10px ; }
742
+ .pending { color : #777 ; }
731
743
```
732
744
733
745
</Sandpack >
734
746
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
+
735
755
---
736
756
737
757
### 显示待处理的视觉状态 {/* displaying-a-pending-visual-state* /}
@@ -803,8 +823,8 @@ export default function TabButton({ action, children, isActive }) {
803
823
}
804
824
return (
805
825
< button onClick= {() => {
806
- startTransition (() => {
807
- action ();
826
+ startTransition (async () => {
827
+ await action ();
808
828
});
809
829
}}>
810
830
{children}
@@ -1094,8 +1114,8 @@ export default function TabButton({ action, children, isActive }) {
1094
1114
}
1095
1115
return (
1096
1116
< button onClick= {() => {
1097
- startTransition (() => {
1098
- action ();
1117
+ startTransition (async () => {
1118
+ await action ();
1099
1119
});
1100
1120
}}>
1101
1121
{children}
@@ -1822,8 +1842,8 @@ import {startTransition} from 'react';
1822
1842
export default function Item ({action}) {
1823
1843
function handleChange (e ) {
1824
1844
// Update the quantity in an Action.
1825
- startTransition (() => {
1826
- action (e .target .value );
1845
+ startTransition (async () => {
1846
+ await action (e .target .value );
1827
1847
});
1828
1848
}
1829
1849
return (
0 commit comments