Files
supabase/packages/common/hooks/useConstant.ts
Ivan Vasilov 1cd1ebfc7f chire: Sort imports in all packages, cms, design-system and ui-library apps (#41610)
Sorted all imports in all packages, `cms`, `design-system` and
`ui-library` apps by running `pnpm format` on them.

All changes in this PR are done by the script.
2026-02-05 13:54:10 +01:00

20 lines
354 B
TypeScript

'use client'
// based on https://github.com/Andarist/use-constant
import { useRef } from 'react'
type ResultBox<T> = { v: T }
/**
* React hook for creating a value exactly once
*/
export function useConstant<T>(fn: () => T): T {
const ref = useRef<ResultBox<T>>()
if (!ref.current) {
ref.current = { v: fn() }
}
return ref.current.v
}