Skip to content

Instantly share code, notes, and snippets.

@Eliav2
Created May 3, 2021 16:36
Show Gist options
  • Save Eliav2/8a3aaac86d891afaacf6e84c9edaae5f to your computer and use it in GitHub Desktop.
Save Eliav2/8a3aaac86d891afaacf6e84c9edaae5f to your computer and use it in GitHub Desktop.
const useLog = (componentName = '', effect = useEffect) => {
// keep track of phase
const render = useRef(0);
const call = useRef(0);
// keep track of how much time from update call to end of effect
const startTime = performance.now();
const callToEffectTime = useRef(0);
const consoleState = () =>
`{call:${call.current},render:${render.current}}(${componentName}) ${callToEffectTime.current}ms`;
const log = (...args) => console.log(...args, consoleState());
effect(() => {
render.current += 1;
callToEffectTime.current = Math.round((performance.now() - startTime) * 100) / 100;
});
call.current += 1;
return log;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment