useMounted hook for checking to see whether a component is mounted
npm install --save @ithreat/use-mounted
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);
})
};
}
MIT © japhy-
This hook is created using create-react-hook.