繁体   English   中英

如何从生产构建中排除钩子

[英]How do I exclude a hook from a production build

我创建了一个打印一些调试信息的钩子,它又调用了一些 Reacts 钩子。 我想自动将其从生产版本中排除。 如果我尝试使用这样的 if 语句来做到这一点:

if (process.env.NODE_ENV === 'development') {
        useMyHook(...)
}

我收到错误React Hook "useMyHook" is called conditionally. React Hooks must be called in the exact same order in every component render React Hook "useMyHook" is called conditionally. React Hooks must be called in the exact same order in every component render 在这种情况下,仍然会按顺序调用挂钩。

如何在生产构建中排除钩子?

您可以使用if (process.env.NODE_ENV === 'development')条件将逻辑包装在useMyHook中。

您可以查看 React 文档中的“挂钩规则”以获得简洁的解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM