Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/m-avagyan/is-mounted-hook
React hook for checking if the component is mounted.
https://github.com/m-avagyan/is-mounted-hook
hook react
Last synced: 10 days ago
JSON representation
React hook for checking if the component is mounted.
- Host: GitHub
- URL: https://github.com/m-avagyan/is-mounted-hook
- Owner: m-avagyan
- License: mit
- Created: 2022-09-12T10:14:08.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-10-03T18:12:17.000Z (about 2 years ago)
- Last Synced: 2024-10-13T01:31:11.504Z (2 months ago)
- Topics: hook, react
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/is-mounted-hook
- Size: 116 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## is-mounted-hook
**React hook for checking if the component is mounted.**
**Author**
- name: _Martik Avagyan_
- email: __
- github: _[m-avagyan](https://github.com/m-avagyan)_## Getting started
**Installation**
`npm install is-mounted-hook` or `yarn add is-mounted-hook`
**Example**
```javascript
import React, { useState, useEffect } from 'react';
import useIsMounted from 'is-mounted-hook';import { getCars } from '../../services/cars';
import Loader from '../loader/Loader';
import Error from '../error/Error';
import CarList from '../car-list/CarList';function Example() {
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState('');
const [data, setData] = useState(null);const isMounted = useIsMounted();
useEffect(() => {
getCars()
.catch((error) => setError(error.message))
.then((data) => {
if (!isMounted.current) {
return;
}setData(data);
})
.finally(() => setIsLoading(false));
}, []);return isLoading ? (
) : error ? (
) : (
);
}export default Example;
```---
**Copyright (c) 2022 [Martik Avagyan](https://github.com/m-avagyan)**