Skip to content

Commit

Permalink
Allow async callback function in useMount #284
Browse files Browse the repository at this point in the history
  • Loading branch information
leroykorterink committed Dec 8, 2023
1 parent 42a69c6 commit 6203d7d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/lifecycle/hooks/useMount/useMount.ts
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
[],
);
}

0 comments on commit 6203d7d

Please sign in to comment.