Skip to content

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

620 B
Raw Blame
import { useState } from 'react'

/**
 * Returns a key that increments each time `isVisible` transitions from false to true.
 * Use this as the `key` prop on a component to force a clean remount on each open,
 * instead of a `useEffect` that imperatively resets internal state.
 */
export function useVisibleKey(isVisible: boolean): number {
  const [key, setKey] = useState(0)
  const [prevIsVisible, setPrevIsVisible] = useState(false)

  if (isVisible && !prevIsVisible) {
    setPrevIsVisible(true)
    setKey((k) => k + 1)
  } else if (!isVisible && prevIsVisible) {
    setPrevIsVisible(false)
  }

  return key
}