UNPKG

231 BJavaScriptView Raw
1import { useRef } from "react";
2
3/**
4 * @param {Function} fn
5 * @return {Any}
6 */
7export default function useConstant(fn) {
8 const ref = useRef();
9 if (!ref.current) {
10 ref.current = { v: fn() };
11 }
12 return ref.current.v;
13}