mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-10-30 00:27:43 +09:00
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.
17 lines
480 B
TypeScript
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();
|
|
});
|
|
};
|