mirror of
https://github.com/streamich/react-use.git
synced 2025-12-08 18:02:14 +00:00
- it has more obvious name; - returns a function that check mount state -> more handy ti use due to less to write; - has tests;
26 lines
484 B
Markdown
26 lines
484 B
Markdown
# `useMountedState`
|
|
|
|
Lifecycle hook providing ability to check component's mount state.
|
|
Gives a function that will return `true` if component mounted and `false` otherwise.
|
|
|
|
## Usage
|
|
|
|
```jsx
|
|
import * as React from 'react';
|
|
import {useMountedState} from 'react-use';
|
|
|
|
const Demo = () => {
|
|
const isMounted = useMountedState();
|
|
|
|
React.useEffect(() => {
|
|
setTimeout(() => {
|
|
if (isMounted()) {
|
|
// ...
|
|
} else {
|
|
// ...
|
|
}
|
|
}, 1000);
|
|
});
|
|
};
|
|
```
|