Files
webstudio/apps/builder/app/shared/hook-utils/use-mount.ts
Bogdan Chadkin da7a3e632c refactor: drop react-use (#3111)
Here replaced react-use utilities with own solutions because could not
bundle it with vite. It also had a lot of transitive deps which bloated
our bundle and node_modules. Win-win.
2024-04-08 00:53:57 +07:00

17 lines
480 B
TypeScript

import { useRef } from "react";
import { type EffectCallback, useEffect } from "react";
export const useMount = (effect: EffectCallback) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(effect, []);
};
export const useUnmount = (fn: () => void): void => {
const fnRef = useRef(fn);
// update the ref each render so if it change the newest callback will be invoked
fnRef.current = fn;
useMount(() => {
return () => fnRef.current();
});
};