Jesus sucks
This commit is contained in:
23
backend/frontend/node_modules/@restart/hooks/esm/useSafeState.js
generated
vendored
Normal file
23
backend/frontend/node_modules/@restart/hooks/esm/useSafeState.js
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user