useSafeState
It is exactly the same with React.useState , but after the component is unmounted, the setState in the asynchronous callback will no longer be executed to avoid memory leakage caused by updating the state after the component is unmounted.
Overview
It is exactly the same with React.useState , but after the component is unmounted, the setState in the asynchronous callback will no longer be executed to avoid memory leakage caused by updating the state after the component is unmounted.
Installation
Open inpnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSafeState.jsonnpx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSafeState.jsonyarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSafeState.jsonbun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSafeState.jsonIt is exactly the same with React.useState , but after the component is unmounted, the setState in the asynchronous callback will no longer be executed to avoid memory leakage caused by updating the state after the component is unmounted.
Examples
Basic usage
API
const [state, setState] = useSafeState(initialState);