-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow async callback function in useMount #284
- Loading branch information
1 parent
42a69c6
commit 6203d7d
Showing
1 changed file
with
13 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* React lifecycle hook that calls a function after the component is mounted. | ||
* @param callbackFunction function to be called when the component is mounted | ||
* | ||
* @example | ||
* ``` | ||
* useMount(() => { | ||
* console.log('component is mounted'); | ||
* }) | ||
* ``` | ||
* | ||
* useMount(async () => { | ||
* console.log('component is mounted'); | ||
* }) | ||
*/ | ||
|
||
export function useMount(callbackFunction: () => void): void { | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useEffect(callbackFunction, []); | ||
export function useMount(callbackFunction: () => void | PromiseLike<void>): void { | ||
useEffect( | ||
() => { | ||
callbackFunction(); | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[], | ||
); | ||
} |