Created
July 18, 2022 13:01
-
-
Save srph/b96d3e1b74fc595432ea069fb8694ebc to your computer and use it in GitHub Desktop.
React: useMount that works with Suspense / Fast Refresh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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