UNPKG

rc-hooks

Version:
15 lines (14 loc) 598 B
/** * 用法与 React.useState 完全一样,但是在组件卸载后 setState 不再执行,避免因组件卸载后更新状态而导致的内存泄漏。 * * @param {*} initialState 状态初始值。 * @example * const [count, setCount] = useSafeState(0); * // 当组件卸载后,调用 `setCount` 不会触发。 */ declare function useSafeState<S>(initialState: S | (() => S)): [S, React.Dispatch<React.SetStateAction<S>>]; declare function useSafeState<S = undefined>(): [ S | undefined, React.Dispatch<React.SetStateAction<S | undefined>> ]; export default useSafeState;