2023-06-27 12:19:06 -04:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-06-27 12:19:06 -04:00
|
|
|
|
2025-02-18 17:59:58 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-06 10:22:13 +02:00
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-06-27 12:19:06 -04:00
|
|
|
*/
|
|
|
|
|
|
2024-05-02 18:44:36 -04:00
|
|
|
import { useRef } from "react";
|
2023-06-27 12:19:06 -04:00
|
|
|
|
2024-05-02 18:44:36 -04:00
|
|
|
/**
|
|
|
|
|
* React hook that returns the value given on the initial render.
|
|
|
|
|
*/
|
|
|
|
|
export function useInitial<T>(getValue: () => T): T {
|
2025-01-13 14:54:42 +00:00
|
|
|
const ref = useRef<{ value: T }>(undefined);
|
|
|
|
|
// only evaluate `getValue` if the ref is undefined
|
2024-05-02 18:44:36 -04:00
|
|
|
ref.current ??= { value: getValue() };
|
|
|
|
|
return ref.current.value;
|
2023-06-27 12:19:06 -04:00
|
|
|
}
|