With React Hooks, what is the best practice for storing a constant that requires significant computation to initialize but then doesn't change across re-renders?
Option 1: Constant Outside of Hook Definition
const x = heavyCalculation();
export default function MyHook() {
return <div>x</div>
}
Option 2: useMemo
export default function MyHook() {
const x = useMemo(() => heavyCalculation(), [])
return <div>x</div>
}
Option 3: useRef
export default function MyHook() {
const x = useRef(heavyCalculation())
return <div>x</div>
}
I like Option 1, but I don't know if it's considered the best for hooks stylistically.