Skip to content
Snippets Groups Projects
Commit ec098d87 authored by John Sparger's avatar John Sparger
Browse files

Add MountEffects hooks to run effects only on or after component mount

parent 4e476942
No related branches found
No related tags found
2 merge requests!139Merge before release,!111Reimplement search to be more responsive
import { useEffect, useRef } from "react";
function useMountEffect(effect, deps=[], afterMount) {
const didMountRef = useRef(false);
useEffect(() => {
let cleanup = null;
if (didMountRef.current === afterMount) {
cleanup = effect();
}
didMountRef.current = true;
return cleanup;
}, deps);
}
export function useEffectOnMount(effect) {
useMountEffect(effect, [], false);
}
export function useEffectAfterMount(effect, deps) {
useMountEffect(effect, deps, true);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment