Created
June 3, 2019 23:35
-
-
Save codeocelot/1591f3d177c9944a26031f53d232db44 to your computer and use it in GitHub Desktop.
useWhy hook
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
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