Skip to content

Commit

Permalink
feat(useIsMounted): allow to set initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
xobotyi committed Jun 30, 2022
1 parent bf99ca6 commit a63188a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/useIsMounted/__docs__/story.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ detect component mount state within async effects.
## Reference

```ts
function useIsMounted(): () => boolean;
function useIsMounted(initialValue = false): () => boolean;
```

#### Importing

<ImportPath />

#### Arguments

- **initialState** _`boolean`_ _(default: `false`)_ - Initial value. By default, this hook
assumes that hook is not mounted yet at first render.

#### Return

Function that yields `true` only in case component is mounted.
7 changes: 5 additions & 2 deletions src/useIsMounted/useIsMounted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import { useCallback, useEffect, useRef } from 'react';
* Returned function yields `true` only in case component is mounted. This hook
* is handy for the cases when you have to detect component mount state within
* async effects.
*
* @param initialValue Initial value. By default, this hook assumes that hook is
* not mounted yet at first render.
*/
export function useIsMounted(): () => boolean {
const isMounted = useRef(false);
export function useIsMounted(initialValue = false): () => boolean {
const isMounted = useRef(initialValue);
const get = useCallback(() => isMounted.current, []);

useEffect(() => {
Expand Down

0 comments on commit a63188a

Please sign in to comment.