ahooks
Version:
react hooks library
17 lines (16 loc) • 464 B
JavaScript
import { useRef } from 'react';
import depsAreSame from '../utils/depsAreSame';
const useCreation = (factory, deps) => {
const { current } = useRef({
deps,
obj: undefined,
initialized: false,
});
if (current.initialized === false || !depsAreSame(current.deps, deps)) {
current.deps = deps;
current.obj = factory();
current.initialized = true;
}
return current.obj;
};
export default useCreation;