简体   繁体   English

在 React 的基于类的组件中使用带有钩子的功能组件是否安全?

[英]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.

相关问题 类组件内的功能组件 - Functional Components inside class components React:从基于 class 的组件更改为基于功能的组件 - React: change from class based components to functional based components 将 react 中的功能组件转换为基于 class 的组件 - Conversion of functional components in react into class based component 何时使用基于 ES6 class 的 React 组件与功能性 ES6 React 组件? - When to use ES6 class based React components vs. functional ES6 React components? React 类内部与外部的功能组件 - Functional components inside vs outside of React class 如何使用 React 钩子测试功能组件内部的功能? - How to test functions inside functional components with React hooks? 我可以将 class 基础组件放入 function 基础组件中以使用反应挂钩吗? - Can I put class base components inside function base components to use react hooks? 将代码从功能性 React 钩子转换为类组件 - Converting code from functional React hooks to class components 为什么使用无状态功能组件而不是基于类的组件? - Why use stateless functional Components instead of class-based Components? 我们可以在React中将类组件用作功能组件的子代吗? - Can we use class components as children for functional components in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM