nextui/apps/docs/hooks/use-update-effect.ts
WK 8c2613713a
refactor: migrate eslint to v9 (#5267)
* refactor: migrate eslint to v9

* chore: lint

* chore: update eslint command

* chore: fix lint warnings

* chore: separate lint and lint:fix

* chore: exclude contentlayer generated code

* fix(scripts): add missing await
2025-06-01 13:51:30 -03:00

29 lines
637 B
TypeScript

import {useEffect, useRef} from "react";
/**
* React effect hook that invokes only on update.
* It doesn't invoke on mount
*/
export const useUpdateEffect: typeof useEffect = (effect, deps) => {
const renderCycleRef = useRef(false);
const effectCycleRef = useRef(false);
useEffect(() => {
const isMounted = renderCycleRef.current;
const shouldRun = isMounted && effectCycleRef.current;
if (shouldRun) {
return effect();
}
effectCycleRef.current = true;
}, deps);
useEffect(() => {
renderCycleRef.current = true;
return () => {
renderCycleRef.current = false;
};
}, []);
};