Outlook_Addin_LLM/node_modules/@fluentui/react-hooks/lib/useConst.js

27 lines
1.3 KiB
JavaScript
Raw Normal View History

import * as React from 'react';
/**
* Hook to initialize and return a constant value. Unlike `React.useMemo`, this is guaranteed to
* always return the same value (and if the initializer is a function, only call it once).
* This is similar to setting a private member in a class constructor.
*
* If the value should ever change based on dependencies, use `React.useMemo` instead.
*
* @param initialValue - Initial value, or function to get the initial value. Similar to `useState`,
* only the value/function passed in the first time this is called is respected.
* @returns The value. The identity of this value will always be the same.
*/
export function useConst(initialValue) {
// Use useRef to store the value because it's the least expensive built-in hook that works here
// (we could also use `const [value] = React.useState(initialValue)` but that's more expensive
// internally due to reducer handling which we don't need)
var ref = React.useRef();
if (ref.current === undefined) {
// Box the value in an object so we can tell if it's initialized even if the initializer
// returns/is undefined
ref.current = {
value: typeof initialValue === 'function' ? initialValue() : initialValue,
};
}
return ref.current.value;
}
//# sourceMappingURL=useConst.js.map