Appearance
useSafeState
A useState whose setter is a no-op once the component has unmounted.
Demo
Usage
tsx
import { useSafeState } from '@reause/shared'
const [value, setValue] = useSafeState(0)
async function load() {
const data = await fetchData()
// ignored if the component unmounted while the request was in flight
setValue(data)
}Type Declarations
ts
export function useSafeState<S>(initialState: S | (() => S)): [
S,
Dispatch<SetStateAction<S>>
]
export function useSafeState<S = undefined>(): [
S | undefined,
Dispatch<SetStateAction<S | undefined>>
]