startTransition
允许你在不阻塞 UI 的情况下更新状态。
¥startTransition
lets you update the state without blocking the UI.
startTransition(scope)
参考
¥Reference
startTransition(scope)
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
-
scope
:通过调用一个或多个set
函数 React 来更新某些状态的函数会立即调用不带参数的scope
,并将在scope
函数调用期间同步安排的所有状态更新标记为 Transitions。它们将是 non-blocking 且 不会显示不需要的加载指示器。¥
scope
: A function that updates some state by calling one or moreset
functions. React immediately callsscope
with no arguments and marks all state updates scheduled synchronously during thescope
function call as Transitions. They 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
的函数必须是同步的。React 立即执行此函数,将其执行时发生的所有状态更新标记为 Transitions。如果你稍后尝试执行更多状态更新(例如,在超时时),它们将不会被标记为 Transition。¥The function you pass to
startTransition
must be synchronous. React immediately executes this function, marking all state updates that happen while it executes as Transitions. If you try to perform more state updates later (for example, in a timeout), they won’t be marked as Transitions. -
标记为 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 will likely 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.