react-use-when-mounted
v1.0.1
Published
React hook which can check if component is still mounted
Downloads
132
Readme
React useWhenMounted
Hook provide callback function that will be invoked only if component is mounted
Getting started
yarn add react-use-when-mounted --save
Usage
import React, { useState, useEffect } from 'react'
import { useWhenMounted } from 'react-use-when-mounted'
export const TestExample: React.FC<{}> = () => {
const whenMounted = useWhenMounted()
const [data, setData] = useState()
useEffect(() => {
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => {
whenMounted(() => {
setData(data)
})
})
},
[whenMounted]
)
return (<div>
{data}
</div>)
}