1.1.0 • Published 3 years ago

@ithreat/use-mounted v1.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

@ithreat/use-mounted

useMounted hook for checking to see whether a component is mounted

NPM JavaScript Style Guide

Install

npm install --save @ithreat/use-mounted

Usage

import React, { useState } from 'react'

import { useMounted } from '@ithreat/use-mounted'

const Example = () => {
  const isMounted = useMounted()
  const [ value, setValue ] = useState()

  const func = (promise) => {
    promise.then((res) => {
      // if component is no longer mounted, stop now
      if (! isMounted()) return;

      // if component is mounted, we can update state
      setValue(res);
    })
  };
}

License

MIT © japhy-


This hook is created using create-react-hook.