startTransition
可让你在后台渲染部分 UI。
¥startTransition
lets you render a part of the UI in the background.
startTransition(action)
参考
¥Reference
startTransition(action)
startTransition
函数允许你将状态更新标记为转换。
¥The startTransition
function lets you mark a state update as a Transition.
import { startTransition } from 'react';
function TabContainer() {
const [tab, setTab] = useState('about');
function selectTab(nextTab) {
startTransition(() => {
setTab(nextTab);
});
}
// ...
}
参数
¥Parameters
-
action
:通过调用一个或多个set
函数 来更新某些状态的函数。React 立即调用action
,不带任何参数,并将action
函数调用期间同步安排的所有状态更新标记为 Transitions。action
中等待的任何异步调用都将包含在转换中,但目前需要在额外的startTransition
中封装await
之后的任何set
函数(参见 故障排除)。标记为 Transitions 的状态更新将是 non-blocking 和 不会显示不需要的加载指示器。。¥
action
: A function that updates some state by calling one or moreset
functions. React callsaction
immediately with no parameters and marks all state updates scheduled synchronously during theaction
function call as Transitions. Any async calls awaited in theaction
will be included in the transition, but currently require wrapping anyset
functions after theawait
in an additionalstartTransition
(see Troubleshooting). State updates marked as Transitions will be non-blocking and will not display unwanted loading indicators..
返回
¥Returns
startTransition
不返回任何东西。
¥startTransition
does not return anything.
注意事项
¥Caveats
-
startTransition
不提供跟踪转换是否待处理的方法。要在转换过程中显示待处理指示器,你需要useTransition
。¥
startTransition
does not provide a way to track whether a Transition is pending. To show a pending indicator while the Transition is ongoing, you needuseTransition
instead. -
只有当你有权访问该状态的
set
函数时,你才能将更新封装到转换中。如果你想在响应某些属性或自定义钩子返回值时启动 Transition,请尝试useDeferredValue
。¥You can wrap an update into a Transition only if you have access to the
set
function of that state. If you want to start a Transition in response to some prop or a custom Hook return value, tryuseDeferredValue
instead. -
你传递给
startTransition
的函数会被立即调用,将其执行时发生的所有状态更新标记为 Transitions。例如,如果你尝试在setTimeout
中执行状态更新,它们将不会被标记为 Transitions。¥The function you pass to the of
startTransition
is called immediately, marking all state updates that happen while it executes as Transitions. If you try to perform state updates in asetTimeout
, for example, they won’t be marked as Transitions. -
你必须在任何异步请求之后将任何状态更新封装在另一个
startTransition
中,以将它们标记为转换。这是一个已知的限制,我们将在未来修复(请参阅 故障排除)。¥You must wrap any state updates after any async requests in another
startTransition
to mark them as Transitions. This is a known limitation that we will fix in the future (see Troubleshooting). -
标记为 Transition 的状态更新将被其他状态更新打断。例如,如果你在转换中更新图表组件,但在图表处于重新渲染过程中时开始输入输入,则 React 将在处理输入状态更新后重新启动图表组件上的渲染工作。
¥A state update marked as a Transition will be interrupted by other state updates. For example, if you update a chart component inside a Transition, but then start typing into an input while the chart is in the middle of a re-render, React will restart the rendering work on the chart component after handling the input state update.
-
转场更新不能用于控制文本输入。
¥Transition updates can’t be used to control text inputs.
-
如果有多个正在进行的 Transition,React 当前会将它们批量处理在一起。这是一个可能会在未来版本中删除的限制。
¥If there are multiple ongoing Transitions, React currently batches them together. This is a limitation that may be removed in a future release.
用法
¥Usage
将状态更新标记为非阻塞转换
¥Marking a state update as a non-blocking Transition
你可以通过将状态更新封装在 startTransition
调用中将其标记为转换:
¥You can mark a state update as a Transition by wrapping it in a startTransition
call:
import { startTransition } from 'react';
function TabContainer() {
const [tab, setTab] = useState('about');
function selectTab(nextTab) {
startTransition(() => {
setTab(nextTab);
});
}
// ...
}
转场让你即使在速度较慢的设备上也能保持用户界面更新的响应。
¥Transitions let you keep the user interface updates responsive even on slow devices.
使用转换,你的 UI 在重新渲染过程中保持响应。例如,如果用户单击一个选项卡但随后改变主意并单击另一个选项卡,则它们可以在不等待第一次重新渲染完成的情况下执行此操作。
¥With a Transition, your UI stays responsive in the middle of a re-render. For example, if the user clicks a tab but then change their mind and click another tab, they can do that without waiting for the first re-render to finish.