Created
June 3, 2024 12:04
-
-
Save alexsad/b64965e50c2ecd47117fcf1e5fe6b199 to your computer and use it in GitHub Desktop.
a use mount utilility to simulate old react didmount
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
import { useEffect, useRef } from "react"; | |
const useMount = (fun: () => Promise<void>, condition = true) => { | |
const firstFlow = useRef(true); | |
useEffect(() => { | |
if (condition && firstFlow.current) { | |
firstFlow.current = false; | |
fun(); | |
} | |
}, [fun, condition]); | |
}; | |
export { useMount }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment