Created
September 13, 2022 09:51
-
-
Save gfox1984/300e5e6387fb6519de86110567549270 to your computer and use it in GitHub Desktop.
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, useInsertionEffect, useCallback } from 'react'; | |
// The useEvent API has not yet been added to React, | |
// so this is a temporary shim to make this sandbox work. | |
// You're not expected to write code like this yourself. | |
export function useEvent(fn) { | |
const ref = useRef(null); | |
useInsertionEffect(() => { | |
ref.current = fn; | |
}, [fn]); | |
return useCallback((...args) => { | |
const f = ref.current; | |
return f(...args); | |
}, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment