Skip to content

Instantly share code, notes, and snippets.

@GalindoSVQ
Created January 7, 2024 10:37
Show Gist options
  • Select an option

  • Save GalindoSVQ/d469a0d256ca775f5f412176a7f7d276 to your computer and use it in GitHub Desktop.

Select an option

Save GalindoSVQ/d469a0d256ca775f5f412176a7f7d276 to your computer and use it in GitHub Desktop.
import * as React from 'react';
React.useEffectEvent = React.experimental_useEffectEvent;
export function useLogger(name, ...rest) {
const initialRenderRef = React.useRef(true);
const handleLog = React.useEffectEvent((event) => {
console.log(`${name} ${event}:`, rest);
});
React.useEffect(() => {
if (initialRenderRef.current === false) {
handleLog('updated');
}
});
React.useEffect(() => {
handleLog('mounted');
initialRenderRef.current = false;
return () => {
handleLog('unmounted');
initialRenderRef.current = true;
};
}, []);
}
@GalindoSVQ

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment