function useThrottledState<TValue, TSelected>(
value,
options,
selector?): [TValue, Dispatch<SetStateAction<TValue>>, ReactThrottler<Dispatch<SetStateAction<TValue>>, TSelected>]
function useThrottledState<TValue, TSelected>(
value,
options,
selector?): [TValue, Dispatch<SetStateAction<TValue>>, ReactThrottler<Dispatch<SetStateAction<TValue>>, TSelected>]
定义于:react-pacer/src/throttler/useThrottledState.ts:93
一个 React hook,它创建一个节流(throttled)的状态值,该值在指定的事件窗口内最多更新一次。该 hook 结合了 React 的 useState 和节流功能,以提供受控的状态更新。
节流确保状态更新以可控的速率发生,无论设置器被调用多少次。这对于限制频繁状态更改带来的昂贵重渲染或依赖于快速变化状态的操作非常有用。
该 hook 返回一个元组,包含:
如果需要对节流进行更直接的控制而无需状态管理,请考虑使用更底层的 useThrottler hook。
该 hook 使用 TanStack Store 通过底层的节流器实例进行响应式状态管理。`selector` 参数允许您指定哪些节流器状态更改将触发重新渲染,通过防止不必要地重新渲染来优化性能,尤其是在发生无关紧要的状态更改时。 仅当您提供 `selector` 时,组件才会因选定状态值的更改而重新渲染。
默认情况下,不会进行任何响应式状态订阅,您必须通过提供 `selector` 函数来选择启用状态跟踪。这可以防止不必要的重新渲染,并让您完全控制组件何时更新。只有当您提供 `selector` 时,组件才会因选定状态值的更改而重新渲染。
可用的节流器状态属性
• TValue
• **TSelected** = ThrottlerState<Dispatch<SetStateAction<TValue>>>
TValue
ThrottlerOptions<Dispatch<SetStateAction<TValue>>>
(state) => TSelected
[TValue, Dispatch<SetStateAction<TValue>>, ReactThrottler<Dispatch<SetStateAction<TValue>>, TSelected>]
// Default behavior - no reactive state subscriptions
const [value, setValue, throttler] = useThrottledState(0, { wait: 1000 });
// Opt-in to re-render when execution count changes (optimized for tracking executions)
const [value, setValue, throttler] = useThrottledState(
0,
{ wait: 1000 },
(state) => ({ executionCount: state.executionCount })
);
// Opt-in to re-render when throttling state changes (optimized for loading indicators)
const [value, setValue, throttler] = useThrottledState(
0,
{ wait: 1000 },
(state) => ({
isPending: state.isPending,
status: state.status
})
);
// Opt-in to re-render when timing information changes (optimized for timing displays)
const [value, setValue, throttler] = useThrottledState(
0,
{ wait: 1000 },
(state) => ({
lastExecutionTime: state.lastExecutionTime,
nextExecutionTime: state.nextExecutionTime
})
);
// With custom leading/trailing behavior
const [value, setValue] = useThrottledState(0, {
wait: 1000,
leading: true, // Update immediately on first change
trailing: false // Skip trailing edge updates
});
// Access throttler methods if needed
const handleReset = () => {
setValue(0);
throttler.cancel(); // Cancel any pending updates
};
// Access the selected throttler state (will be empty object {} unless selector provided)
const { executionCount, isPending } = throttler.state;
// Default behavior - no reactive state subscriptions
const [value, setValue, throttler] = useThrottledState(0, { wait: 1000 });
// Opt-in to re-render when execution count changes (optimized for tracking executions)
const [value, setValue, throttler] = useThrottledState(
0,
{ wait: 1000 },
(state) => ({ executionCount: state.executionCount })
);
// Opt-in to re-render when throttling state changes (optimized for loading indicators)
const [value, setValue, throttler] = useThrottledState(
0,
{ wait: 1000 },
(state) => ({
isPending: state.isPending,
status: state.status
})
);
// Opt-in to re-render when timing information changes (optimized for timing displays)
const [value, setValue, throttler] = useThrottledState(
0,
{ wait: 1000 },
(state) => ({
lastExecutionTime: state.lastExecutionTime,
nextExecutionTime: state.nextExecutionTime
})
);
// With custom leading/trailing behavior
const [value, setValue] = useThrottledState(0, {
wait: 1000,
leading: true, // Update immediately on first change
trailing: false // Skip trailing edge updates
});
// Access throttler methods if needed
const handleReset = () => {
setValue(0);
throttler.cancel(); // Cancel any pending updates
};
// Access the selected throttler state (will be empty object {} unless selector provided)
const { executionCount, isPending } = throttler.state;
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。