Skip to content

Latest commit

 

History

History
18 lines (14 loc) · 487 Bytes

use-is-mounted.md

File metadata and controls

18 lines (14 loc) · 487 Bytes

useIsMounted

Returns a callback function that returns a boolean when called indicating if the current component is still mounted. This is useful for gatekeeping code paths that would set local component state after asynchronous behavior.

Example

const [state, setState] = useState();
const isMounted = useIsMounted();

useEffect(() => {
  someAsyncThing().then(() => {
    if (isMounted()) {
      setState(/* ... */);
    }
  });
}, [/* effect dependencies */]);