UNPKG

@restart/hooks

Version:

A set of utility and general-purpose React hooks.

23 lines (21 loc) 654 B
import { useCallback } from 'react'; import useMounted from './useMounted.js'; /** * `useSafeState` takes the return value of a `useState` hook and wraps the * setter to prevent updates onces the component has unmounted. Can used * with `useMergeState` and `useStateAsync` as well * * @param state The return value of a useStateHook * * ```ts * const [show, setShow] = useSafeState(useState(true)); * ``` */ function useSafeState(state) { const isMounted = useMounted(); return [state[0], useCallback(nextState => { if (!isMounted()) return; return state[1](nextState); }, [isMounted, state[1]])]; } export default useSafeState;