Skip to content

Instantly share code, notes, and snippets.

@alexsad
Created June 3, 2024 12:04
Show Gist options
  • Save alexsad/b64965e50c2ecd47117fcf1e5fe6b199 to your computer and use it in GitHub Desktop.
Save alexsad/b64965e50c2ecd47117fcf1e5fe6b199 to your computer and use it in GitHub Desktop.
a use mount utilility to simulate old react didmount
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