From 6203d7d14b9fc5933d5574d54f0f2368e924349f Mon Sep 17 00:00:00 2001 From: Leroy Korterink Date: Fri, 8 Dec 2023 15:02:28 +0100 Subject: [PATCH] Allow async callback function in useMount #284 --- src/lifecycle/hooks/useMount/useMount.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/lifecycle/hooks/useMount/useMount.ts b/src/lifecycle/hooks/useMount/useMount.ts index c23b620..a7ba392 100644 --- a/src/lifecycle/hooks/useMount/useMount.ts +++ b/src/lifecycle/hooks/useMount/useMount.ts @@ -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 { + useEffect( + () => { + callbackFunction(); + }, + // eslint-disable-next-line react-hooks/exhaustive-deps + [], + ); }