react-use-mounted
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i react-use-mounted

Weekly Downloads

27

Version

2.0.0

License

none

Unpacked Size

3.31 kB

Total Files

8

Last publish

Collaborators

  • fabiospampinato