Last active
February 7, 2024 15:16
-
-
Save rahsheen/5945af79d758e2d2c428f46c1afed48f to your computer and use it in GitHub Desktop.
How to see which Components are calling your hook
This file contains hidden or 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
// Drop this into your hook | |
// Do not wrap in a useEffect | |
function whoIsMyDaddy() { | |
try { | |
throw new Error() | |
} catch (e) { | |
// matches this function, the caller and the parent | |
const allMatches = e.stack.match(/(\w+)@|at (\w+) \(/g) | |
// match parent function name | |
const parentMatches = allMatches[2].match(/(\w+)@|at (\w+) \(/) | |
// return only name | |
return parentMatches[1] || parentMatches[2] | |
} | |
} | |
const daddy = whoIsMyDaddy() | |
console.log("Daddy?", daddy) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment