Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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)**