| name | use-static-effect-event |
| description | useStaticEffectEvent hook in Supabase Studio — a userland polyfill for React's useEffectEvent. Use when you need to read latest state/props inside a useEffect without re-triggering it, or when stale closures in Effects are causing bugs. |
useStaticEffectEvent
Located at apps/studio/hooks/useStaticEffectEvent.ts.
A userland polyfill for React's useEffectEvent (stable in React 19.2). It solves the stale closure problem: gives you a stable callback that always reads the latest props/state without those values triggering Effect re-runs.
The Problem It Solves
Without it, you face two bad options inside useEffect:
- Add values to dependencies → unnecessary Effect re-runs (teardown/reconnect)
- Omit from dependencies → stale closure bugs (outdated values)
useEffect(() => {
const connection = createConnection(roomId)
connection.on('connected', () => {
showNotification('Connected!', theme)
})
return () => connection.disconnect()
}, [roomId, theme])
When to Use
- Read latest state/props inside an Effect without re-triggering it
- Create stable callbacks that always use current values
- Avoid stale closures in event handlers used within Effects
Pattern 1: Sync data without re-running on every change
const syncApiPrivileges = useStaticEffectEvent(() => {
if (hasLoadedInitialData.current) return
if (!apiAccessStatus.isSuccess) return
if (!privilegesForTable) return
hasLoadedInitialData.current = true
setPrivileges(privilegesForTable.privileges)
})
useEffect(() => {
syncApiPrivileges()
}, [apiAccessStatus.status, syncApiPrivileges])
Pattern 2: Stable callbacks for async operations
const exportInternal = useStaticEffectEvent(
async ({ bypassConfirmation }: { bypassConfirmation: boolean }) => {
if (!params.enabled) return
const { projectRef, connectionString, entity, totalRows } = params
}
)
const exportInDesiredFormat = useCallback(
() => exportInternal({ bypassConfirmation: false }),
[exportInternal]
)
Pattern 3: Infinite scroll / pagination triggers
const fetchNext = useStaticEffectEvent(() => {
if (lastItem && lastItem.index >= items.length - 1 && hasNextPage && !isFetchingNextPage) {
fetchNextPage()
}
})
useEffect(fetchNext, [lastItem, fetchNext])
When NOT to Use
Don't use it to hide legitimate dependencies:
const connect = useStaticEffectEvent(() => {
const connection = createConnection(roomId)
connection.connect()
})
useEffect(() => {
connect()
}, [connect])
useEffect(() => {
const connection = createConnection(roomId)
connection.connect()
return () => connection.disconnect()
}, [roomId])
Don't use it for simple event handlers outside Effects:
const handleClick = useStaticEffectEvent(() => console.log(count))
const handleClick = () => console.log(count)
Rules
- Only call the returned function inside Effects (
useEffect, useLayoutEffect)
- Don't pass it to other components or hooks as a callback prop
- Use for non-reactive logic only — reads values but shouldn't trigger re-runs
- Include it in dependency arrays when used in
useEffect (it's stable, won't cause re-runs)
How It Works
export const useStaticEffectEvent = <Callback extends Function>(callback: Callback) => {
const callbackRef = useRef(callback)
useLayoutEffect(() => {
callbackRef.current = callback
})
const eventFn = useCallback((...args: any) => {
return callbackRef.current(...args)
}, [])
return eventFn as unknown as Callback
}