Skip to content

Instantly share code, notes, and snippets.

@codeocelot
Created June 3, 2019 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeocelot/1591f3d177c9944a26031f53d232db44 to your computer and use it in GitHub Desktop.
Save codeocelot/1591f3d177c9944a26031f53d232db44 to your computer and use it in GitHub Desktop.
useWhy hook
import { useState } from 'react';
function useWhy(deps, id = null, logger = console.log) {
const [prevDeps, setPrevDeps] = useState(deps);
let changed = false;
deps.forEach((d, i) => {
if (d !== prevDeps[i]) {
changed = true;
logger(`Why ${id}: dep ${i} has changed from ${prevDeps[i]} to ${d}`);
}
});
if (changed) setPrevDeps(deps);
return deps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment