on master help sign in
bridge/src/lib/useConstant.js
13 lines 284 B history raw
1import { useRef } from 'react';
3// inspired by, but simpler version of:
4// https://github.com/Andarist/use-constant/blob/master/src/index.ts
5export default function useConstant(fn) {
6 const ref = useRef();
8 if (!ref.current) {
9 ref.current = fn();
10 }
12 return ref.current;