Custom hook to manage state that only updates if the component is still mounted.
This hook provides a stateful value and a function to update it, ensuring the state is only updated if the component is still mounted, avoiding potential memory leaks.
Optional
The initial state value or a function that returns the initial state.
const [value, setValue] = useSafeState(0);useEffect(() => { setTimeout(() => { setValue(1); // This will only run if the component is still mounted }, 1000);}, [setValue]); Copy
const [value, setValue] = useSafeState(0);useEffect(() => { setTimeout(() => { setValue(1); // This will only run if the component is still mounted }, 1000);}, [setValue]);
Custom hook to manage state that only updates if the component is still mounted.
This hook provides a stateful value and a function to update it, ensuring the state is only updated if the component is still mounted, avoiding potential memory leaks.