Skip to content

Instantly share code, notes, and snippets.

@gfox1984
Created September 13, 2022 09:51
Show Gist options
  • Save gfox1984/300e5e6387fb6519de86110567549270 to your computer and use it in GitHub Desktop.
Save gfox1984/300e5e6387fb6519de86110567549270 to your computer and use it in GitHub Desktop.
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