Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

365 B
Raw Blame
'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> | null>(null)

  if (!ref.current) {
    ref.current = { v: fn() }
  }

  return ref.current.v
}