简体   繁体   English

React 功能组件与 class 组件

[英]React functional components vs class components

I am a beginner in React learning it for the last 3 months, However, the way to implement components has split into 2 kinds functional and class.我是最近 3 个月学习 React 的初学者,但是,实现组件的方式分为两种功能和 class。 As of 2021 which path to advance in learning, functional Style or class style components.截至 2021 年,学习、功能样式或 class 样式组件的前进路径。

  • How will the development approach will be adapted in the long run maybe next 5 years从长远来看,未来 5 年将如何调整开发方法
  • Based on the style I can focus on design pattern and architecture style for that particular component style.基于风格,我可以专注于特定组件风格的设计模式和架构风格。

For the long run, you should focus on functional components, as react team has suggested to use functional components with hooks to manage the state for component.从长远来看,您应该关注功能组件,因为 React 团队建议使用带有钩子的功能组件来管理组件的 state。

But that does not mean you should completely ignore class components.但这并不意味着您应该完全忽略 class 组件。 The reason is in professional life you may come up with the code base that was written before the introduction of hooks using class components and you are not allowed to refactor it to functional components ( there can be various reasons of not doing that) in that case your knowledge about class components will come in handy.原因是在职业生涯中,您可能会想出在引入使用 class 组件的钩子之前编写的代码库,并且在这种情况下,您不允许将其重构为功能组件(可能有各种原因不这样做)您对 class 组件的了解将派上用场。

I don't want to mention other people, I want to share my experience, ill Use the class component when I want call an action or API call, and when I want state change and flexible data change use the functional component!我不想提及其他人,我想分享我的经验,生病了当我想调用一个动作或 API 调用时使用 class 组件,当我想要 state 时使用更改和灵活的数据更改组件

Look at this example and you will find which one is better:看看这个例子,你会发现哪个更好:

Note : Also Check when you pass the same value inside the input注意:还要检查您何时在输入中传递相同的值

Class component: Class 组件:

 let AppRender = 0; class App extends React.Component { state = { hello: "" } setHello = (value) => this.setState({hello:value}) render() { return( <div> <h2>Hello {this.state.hello}</h2> <p>AppRender: {++AppRender}</p> <Child setHello={this.setHello}/> </div> ) } } let ChildRender = 0; class Child extends React.Component { state = { input: "World" } buttonHandler = () => { this.props.setHello(this.state.input) } inputHandler = (e) => { this.setState({input:e.target.value}) } componentDidMount() { this.props.setHello(this.state.input) }; render() { return ( <div> <p>ChildRender: {++ChildRender}</p> <input type="text" value={this.state.input} onChange={this.inputHandler}></input> <button onClick={this.buttonHandler}>Say Hello</button> </div> ) } } ReactDOM.render(<App />, document.getElementById("react"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="react"></div>

Functional component:功能组件:

 let AppRender = 0; const App = () => { const [hello, setHello] = React.useState("") return( <div> <h2>Hello {hello}</h2> <p>AppRender: {++AppRender}</p> <Child setHello={setHello}/> </div> ) } let ChildRender = 0; const Child = ({setHello}) => { const [input, setInput] = React.useState("World") const buttonHandler = () => { setHello(input) } const inputHandler = (e) => { setInput(e.target.value) } React.useEffect(() => { setHello(input) }, []); return( <div> <p>ChildRender: {++ChildRender}</p> <input type="text" value={input} onChange={inputHandler}></input> <button onClick={buttonHandler}>Say Hello</button> </div> ) } ReactDOM.render(<App />, document.getElementById("react"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="react"></div>

I think the question you are asking is hooks vs classes, because functional components have existed since the beginning of React.我认为您要问的问题是钩子与类,因为自 React 开始以来就存在功能组件。 Before hooks, there were certain limitations to what you can do with functional components and you had to use class components to make a React application.在 hooks 之前,你可以使用功能组件做一些限制,你必须使用 class 组件来制作 React 应用程序。 But since hooks were introduced now you can do everything you could with classes in a functional component.但是由于现在引入了钩子,您可以对功能组件中的类做所有可以做的事情。

Hooks is definitely the way to go right now. Hooks 绝对是现在通往 go 的方式。 Its even mentioned in the React documentation itself:它甚至在 React 文档本身中提到:

In the longer term, we expect Hooks to be the primary way people write React components.从长远来看,我们希望 Hooks 成为人们编写 React 组件的主要方式。

https://reactjs.org/docs/hooks-faq.html#should-i-use-hooks-classes-or-a-mix-of-both https://reactjs.org/docs/hooks-faq.html#should-i-use-hooks-classes-or-a-mix-of-both

There's really little to choose between them but you'll find that a lot of tutorials and guides are now written for functional components and, for someone who's just starting out, you might want to stick with what you can easily find relevant information for.它们之间几乎没有可供选择的地方,但您会发现现在有很多教程和指南都是针对功能组件编写的,对于刚入门的人来说,您可能希望坚持使用可以轻松找到相关信息的内容。 Also, even the React team are focusing more on functional components and I strongly believe class components would be quietly phased out over the next couple years so maybe go with functional components.此外,即使是 React 团队也更多地关注功能组件,我坚信 class 组件将在未来几年内悄然淘汰,所以 go 可能会带有功能组件。

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

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