https://github.com/hupe1980/react-is-mounted-hook
React hook to check if the component is still mounted
https://github.com/hupe1980/react-is-mounted-hook
hook react
Last synced: 6 months ago
JSON representation
React hook to check if the component is still mounted
- Host: GitHub
- URL: https://github.com/hupe1980/react-is-mounted-hook
- Owner: hupe1980
- License: mit
- Created: 2019-06-01T15:00:35.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-17T17:59:07.000Z (over 2 years ago)
- Last Synced: 2025-03-28T19:44:43.021Z (6 months ago)
- Topics: hook, react
- Language: TypeScript
- Homepage:
- Size: 1.02 MB
- Stars: 16
- Watchers: 2
- Forks: 2
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# react-is-mounted-hook
> React hook to check if the component is still mounted
## Install
```sh
// with npm
npm install react-is-mounted-hook// with yarn
yarn add react-is-mounted-hook
```## How to use
```javascript
import React, { useState, useEffect } from 'react';
import useIsMounted from 'react-is-mounted-hook';
import axios from 'axios';
import Loading from './loading';
import Result from './result';const FetchComponent = () => {
const isMounted = useIsMounted();
const [data, setData] = useState(null);useEffect(() => {
const fetchData = async () => {
const result = await axios(
'http://hn.algolia.com/api/v1/search?query=redux'
);
if (isMounted()) {
setData(result.data);
}
};fetchData();
}, [isMounted]);return data ? : ;
};export default FetchComponent;
```## License
[MIT](LICENSE)