Last active
January 31, 2020 11:38
-
-
Save vzaidman/2996cfd9f0ff9ad50dbcefcad2f2fb3c to your computer and use it in GitHub Desktop.
Advanced React- useCallback() Invalidates Too Often in Practice #14099
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
/* | |
Notice: This hook would be problematic in concurrent mode: | |
https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback | |
*/ | |
function useEventCallback(fn, dependencies) { | |
const ref = React.useRef(() => { | |
throw new Error('Cannot call an event handler while rendering.'); | |
}); | |
React.useEffect(() => { | |
ref.current = fn; | |
}, [fn, ...dependencies]); | |
return React.useCallback((...args) => (0, ref.current)(...args), []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment