- import * as React from "react";
- // Why doesn't React have this functionality built-in? (and implemented more efficiently than hacking with useRef)
- export function useConst<T>(initialCb: ()=>T): T {
- const ref = React.useRef<T>();
- if (ref.current === undefined) {
- const initialValue = initialCb();
- ref.current = initialValue;
- }
- return ref.current!;
- }
|