Skip to content

Instantly share code, notes, and snippets.

@rahsheen
Last active February 7, 2024 15:16
Show Gist options
  • Save rahsheen/5945af79d758e2d2c428f46c1afed48f to your computer and use it in GitHub Desktop.
Save rahsheen/5945af79d758e2d2c428f46c1afed48f to your computer and use it in GitHub Desktop.
How to see which Components are calling your hook
// 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