Created
July 5, 2019 03:45
-
-
Save arminyahya/9ddeb23899baf09f2ea581f38c7a4bef to your computer and use it in GitHub Desktop.
useInstance hook
This file contains 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 { useRef } from 'react'; | |
export const useInstance = <T>(fn: () => T) => { | |
const instanceRef = useRef<T | null>(null); | |
if (instanceRef.current === null) { | |
instanceRef.current = fn(); | |
} | |
return instanceRef.current as T; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment