volleyball-dev-frontend/node_modules/@restart/hooks/esm/useSafeState.js
2025-06-02 16:42:16 +00:00

23 lines
651 B
JavaScript

import { useCallback } from 'react';
import useMounted from './useMounted';
/**
* `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;