[英]How to find the component calling a custom hook in React?
I've a custom hook being called by multiple components, including other custom hooks.我有一个自定义挂钩被多个组件调用,包括其他自定义挂钩。 I added an useEffect in the custom hook to run on only initial render with
console.trace()
, to figure out the caller component.我在自定义挂钩中添加了一个 useEffect 以仅在使用
console.trace()
的初始渲染上运行,以找出调用者组件。
useEffect(() => {
console.trace()
}, [])
But this logs the stack trace with invokePassiveCreate
at the top.但这会在顶部记录带有
invokePassiveCreate
的堆栈跟踪。 I can't find the caller from the stack trace.我无法从堆栈跟踪中找到调用者。 Is there another way to figure it out?
还有另一种方法可以解决吗?
useEffect is invoked by React so here we did not get the caller of the custom hook or any component. useEffect 是由 React 调用的,所以这里我们没有得到自定义钩子或任何组件的调用者。 use console.trace() outside useEffect.
在 useEffect 之外使用 console.trace()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.