You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react/useImperativeHandle.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ function MyInput({ ref }) {
46
46
47
47
<Note>
48
48
49
-
Starting with React 19, [`ref`is available as a prop.](/blog/2024/12/05/react-19#ref-as-a-prop) In React 18 and earlier, it was necessary to get the `ref` from [`forwardRef`.](/reference/react/forwardRef)
The function passed to `startTransition`is called an "Action". By convention, any callback called inside `startTransition`(such as a callback prop) should be named `action`or include the "Action" suffix:
@@ -95,7 +95,7 @@ function SubmitButton({ submitAction }) {
95
95
96
96
#### 参数 {/*starttransition-parameters*/}
97
97
98
-
*`action`: A function that updates some state by calling one or more [`set`functions](/reference/react/useState#setstate). React calls`action` immediately with no parameters and marks all state updates scheduled synchronously during the `action`function call as Transitions. Any async calls that are awaited in the `action`will be included in the Transition, but currently require wrapping any `set` functions after the `await` in an additional `startTransition`(see [Troubleshooting](#react-doesnt-treat-my-state-update-after-await-as-a-transition)). State updates marked as Transitions will be [non-blocking](#marking-a-state-update-as-a-non-blocking-transition) and [will not display unwanted loading indicators](#preventing-unwanted-loading-indicators).
The function passed to `startTransition`is called the "Action". You can update state and (optionally) perform side effects within an Action, and the work will be done in the background without blocking user interactions on the page. A Transition can include multiple Actions, and while a Transition is in progress, your UI stays responsive. For example, if the user clicks a tab but then changes their mind and clicks another tab, the second click will be immediately handled without waiting for the first update to finish.
To give the user feedback about in-progress Transitions, to `isPending`state switches to `true` at the first call to `startTransition`, and stays `true`until all Actions complete and the final state is shown to the user. Transitions ensure side effects in Actions to complete in order to [prevent unwanted loading indicators](#preventing-unwanted-loading-indicators), and you can provide immediate feedback while the Transition is in progress with `useOptimistic`.
In this example, the `updateQuantity`function simulates a request to the server to update the item's quantity in the cart. This function is *artificially slowed down* so that it takes at least a second to complete the request.
Update the quantity multiple times quickly. Notice that the pending "Total" state is shown while any requests are in progress, and the "Total" updates only after the final request is complete. Because the update is in an Action, the "quantity" can continue to be updated while the request is in progress.
@@ -305,22 +305,22 @@ export async function updateQuantity(newQuantity) {
305
305
306
306
</Sandpack>
307
307
308
-
This is a basic example to demonstrate how Actions work, but this example does not handle requests completing out of order. When updating the quantity multiple times, it's possible for the previous requests to finish after later requests causing the quantity to update out of order. This is a known limitation that we will fix in the future (see [Troubleshooting](#my-state-updates-in-transitions-are-out-of-order) below).
These solutions handle request ordering for you. When using Transitions to build your own custom hooks or libraries that manage async state transitions, you have greater control over the request ordering, but you must handle it yourself.
In this example, the `updateQuantity`function also simulates a request to the server to update the item's quantity in the cart. This function is *artificially slowed down* so that it takes at least a second to complete the request.
Update the quantity multiple times quickly. Notice that the pending "Total" state is shown while any requests is in progress, but the "Total" updates multiple times for each time the "quantity" was clicked:
@@ -447,7 +447,7 @@ export async function updateQuantity(newQuantity) {
447
447
448
448
</Sandpack>
449
449
450
-
A common solution to this problem is to prevent the user from making changes while the quantity is updating:
450
+
针对此问题的常见解决方案是在数量更新期间阻止用户进行更改:
451
451
452
452
<Sandpack>
453
453
@@ -573,7 +573,7 @@ export async function updateQuantity(newQuantity) {
573
573
574
574
</Sandpack>
575
575
576
-
This solution makes the app feel slow, because the user must wait each time they update the quantity. It's possible to add more complex handling manually to allow the user to interact with the UI while the quantity is updating, but Actions handle this case with a straight-forward built-in API.
576
+
这种解决方案会让应用显得卡顿,因为用户必须每次更新数量时都等待。虽然可以手动添加更复杂的处理逻辑来允许用户在更新数量时继续操作界面,但 React Actions 通过直观的内置 API 就能轻松处理这种情况。
577
577
578
578
<Solution />
579
579
@@ -1542,9 +1542,9 @@ main {
1542
1542
1543
1543
---
1544
1544
1545
-
### Displaying an error to users with an error boundary {/*displaying-an-error-to-users-with-error-boundary*/}
If a function passed to `startTransition`throws an error, you can display an error to your user with an [error boundary](/reference/react/Component#catching-rendering-errors-with-an-error-boundary). To use an error boundary, wrap the component where you are calling the `useTransition`in an error boundary. Once the function passed to `startTransition`errors, the fallback for the error boundary will be displayed.
When you use `await` inside a `startTransition` function, the state updates that happen after the `await`are not marked as Transitions. You must wrap state updates after each `await`in a `startTransition`call:
This is a JavaScript limitation due to React losing the scope of the async context. In the future, when [AsyncContext](https://github.com/tc39/proposal-async-context) is available, this limitation will be removed.
If you `await` inside `startTransition`, you might see the updates happen out of order.
1760
1759
1761
-
In this example, the `updateQuantity` function simulates a request to the server to update the item's quantity in the cart. This function *artificially returns the every other request after the previous* to simulate race conditions for network requests.
@@ -1928,8 +1929,8 @@ export async function updateQuantity(newName) {
1928
1929
</Sandpack>
1929
1930
1930
1931
1931
-
When clicking multiple times, it's possible for previous requests to finish after later requests. When this happens, React currently has no way to know the intended order. This is because the updates are scheduled asynchronously, and React loses context of the order across the async boundary.
This is expected, because Actions within a Transition do not guarantee execution order. For common use cases, React provides higher-level abstractions like [`useActionState`](/reference/react/useActionState) and [`<form>` actions](/reference/react-dom/components/form) that handle ordering for you. For advanced use cases, you'll need to implement your own queuing and abort logic to handle this.
0 commit comments