npm.io
2.0.0 • Published 4 years ago

react-use-mounted

Licence
Version
2.0.0
Deps
0
Size
3 kB
Vulns
0
Weekly
0
Stars
2

React - useMounted

React hook for checking if the component is mounted.

Install

npm install --save react-use-mounted

Usage

import {useState} from 'react';
import useMounted from 'react-use-mounted';

function MyComponent () {

  const [foo, setFoo] = useState ( false );
  const mounted = useMounted ();

  useWhatever ( () => {
    if ( !mounted.current ) return; // Not mounted, avoiding calling `setFoo`
    setFoo ( true );
  });

}

License

MIT Fabio Spampinato

Keywords