简体   繁体   English

我如何理解 React.Component 和高阶组件之间的区别?

[英]How can I understand differences between React.Component and Higher-Order Components?

I have used to this code style before.我以前已经习惯了这种代码风格。

import React from 'react';

class App extends React.Component {
  render() {
    return (
      <>This is my app</>
    );
  }
}

But now, I see all things are Higher-Order Components.但是现在,我看到所有东西都是高阶组件。

import React from 'react';

function App() {
  return (
    <>This is my app</>
  );
}

I haven't understood about them.我对他们不了解。 Sometimes, I read an example written in the class style and can't convert ideas into the Higher-Order Components.有时,我阅读了一个以 class 风格编写的示例,并且无法将想法转换为高阶组件。 Please explain to me or give me some related keywords, posts or other types of document?请给我解释或给我一些相关的关键字、帖子或其他类型的文件?

Thank you!谢谢!

I think I figured out my problem.我想我发现了我的问题。

  • Firstly, they are differences between Class and Function Component, not Higher-Order Component.首先,它们是 Class 和 Function 组件之间的差异,而不是高阶组件。

  • Secondly, to convert Class component to Function component, I have to understand how to add state and lifecycle methods to both of them.其次,要将 Class 组件转换为 Function 组件,我必须了解如何将 state 和生命周期方法添加到它们两者。

https://reactjs.org/docs/state-and-lifecycle.html https://reactjs.org/docs/state-and-lifecycle.html
https://itnext.io/add-state-and-lifecycle-methods-to-function-components-with-react-hooks-8e2bdc44d43d https://itnext.io/add-state-and-lifecycle-methods-to-function-components-with-react-hooks-8e2bdc44d43d

Thanks for giving some useful comments!感谢您提供一些有用的意见!

Question is closed.问题已关闭。

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

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