shadcn-ahooks

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.

Documentation and Examples

Installation

Open in
pnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSafeState.json
npx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSafeState.json
yarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSafeState.json
bun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useSafeState.json

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.

Examples

Basic usage

API

const [state, setState] = useSafeState(initialState);

On this page