[英]Is that safe to use functional components with hooks inside class based components in React?
I have a question regarding the React Hooks and class based components.我有一个关于 React Hooks 和基于类的组件的问题。 I've heard from some developers that using functional components with React Hooks as a children of class based components may lead to some tricky bugs and that it's better to avoid doing so .
我从一些开发人员那里听说,使用带有 React Hooks 的函数式组件作为基于类的组件的子级可能会导致一些棘手的错误,最好避免这样做。 I don't have more information about that and didn't find any articles or something like that.
我没有关于这方面的更多信息,也没有找到任何文章或类似的东西。
So does anyone knows if it's true or not?那么有没有人知道这是真的还是假的?
And if it's true please give me some examples or at least explain that to me.如果这是真的,请给我一些例子,或者至少向我解释一下。
I think the main reason of it is that we should stick with one style per project.我认为主要原因是我们应该坚持每个项目一种风格。 I mean it is better to use only hooks (new projects) or only class components (for example in older projects) because doing 2 similar things in the same time will be hard for not experienced developers.
我的意思是最好只使用钩子(新项目)或只使用类组件(例如在旧项目中),因为对于没有经验的开发人员来说,同时做两件类似的事情会很困难。 But this is only my personal opinion.
但这只是我个人的意见。
Regarding your problem I think you have mistook it with this sentence "You can't use Hooks inside of a class component".关于您的问题,我认为您将其误认为是“您不能在类组件内使用 Hooks”这句话。 That means you can not apply hook to class component, but it is safe to render hook based component inside class.
这意味着您不能将钩子应用于类组件,但在类内部呈现基于钩子的组件是安全的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.