JSPM

react-use-mounted

2.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q50305F

React hook for checking if the component is mounted.

Package Exports

  • react-use-mounted

Readme

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