Skip to content

Instantly share code, notes, and snippets.

@srph
Created July 18, 2022 13:01
Show Gist options
  • Save srph/b96d3e1b74fc595432ea069fb8694ebc to your computer and use it in GitHub Desktop.
Save srph/b96d3e1b74fc595432ea069fb8694ebc to your computer and use it in GitHub Desktop.
React: useMount that works with Suspense / Fast Refresh
import { useEffect, useRef } from 'react'
// useEffect that runs on mount and works with Suspense / Fast Refresh
const useMount = (fn: () => void) => {
const isMountedRef = useRef(false)
useEffect(() => {
if (isMountedRef.current) return
isMountedRef.current = true
fn()
}, [])
}
export { useMount }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment