面试官:说说对React中类组件和函数组件的理解?有什么区别?

开发 前端
类组件,顾名思义,也就是通过使用ES6类的编写形式去编写组件,该类必须继承React.Component。

[[410052]]

本文转载自微信公众号「JS每日一题」,作者灰灰。转载本文请联系JS每日一题公众号。

一、类组件

类组件,顾名思义,也就是通过使用ES6类的编写形式去编写组件,该类必须继承React.Component

如果想要访问父组件传递过来的参数,可通过this.props的方式去访问

在组件中必须实现render方法,在return中返回React对象,如下:

  1. class Welcome extends React.Component { 
  2.   constructor(props) { 
  3.     super(props) 
  4.   } 
  5.   render() { 
  6.     return <h1>Hello, {this.props.name}</h1> 
  7.   } 

二、函数组件

函数组件,顾名思义,就是通过函数编写的形式去实现一个React组件,是React中定义组件最简单的方式

  1. function Welcome(props) { 
  2.   return <h1>Hello, {props.name}</h1>; 

函数第一个参数为props用于接收父组件传递过来的参数

三、区别

针对两种React组件,其区别主要分成以下几大方向:

  • 编写形式
  • 状态管理
  • 生命周期
  • 调用方式
  • 获取渲染的值

编写形式

两者最明显的区别在于编写形式的不同,同一种功能的实现可以分别对应类组件和函数组件的编写形式

函数组件:

  1. function Welcome(props) { 
  2.   return <h1>Hello, {props.name}</h1>; 

类组件:

  1. cass Welcome extends React.Component { 
  2.   constructor(props) { 
  3.     super(props) 
  4.   } 
  5.   render() { 
  6.     return <h1>Hello, {this.props.name}</h1> 
  7.   } 

状态管理

在hooks出来之前,函数组件就是无状态组件,不能保管组件的状态,不像类组件中调用setState

如果想要管理state状态,可以使用useState,如下:

  1. const FunctionalComponent = () => { 
  2.     const [count, setCount] = React.useState(0); 
  3.  
  4.     return ( 
  5.         <div> 
  6.             <p>count: {count}</p> 
  7.             <button onClick={() => setCount(count + 1)}>Click</button> 
  8.         </div> 
  9.     ); 
  10. }; 

在使用hooks情况下,一般如果函数组件调用state,则需要创建一个类组件或者state提升到你的父组件中,然后通过props对象传递到子组件

生命周期

在函数组件中,并不存在生命周期,这是因为这些生命周期钩子都来自于继承的React.Component

所以,如果用到生命周期,就只能使用类组件

但是函数组件使用useEffect也能够完成替代生命周期的作用,这里给出一个简单的例子:

  1. const FunctionalComponent = () => { 
  2.     useEffect(() => { 
  3.         console.log("Hello"); 
  4.     }, []); 
  5.     return <h1>Hello, World</h1>; 
  6. }; 

上述简单的例子对应类组件中的componentDidMount生命周期

如果在useEffect回调函数中return一个函数,则return函数会在组件卸载的时候执行,正如componentWillUnmount

  1. const FunctionalComponent = () => { 
  2.  React.useEffect(() => { 
  3.    return () => { 
  4.      console.log("Bye"); 
  5.    }; 
  6.  }, []); 
  7.  return <h1>Bye, World</h1>; 
  8. }; 

调用方式

如果是一个函数组件,调用则是执行函数即可:

  1. // 你的代码  
  2. function SayHi() {  
  3.     return <p>Hello, React</p>  
  4. }  
  5. // React内部  
  6. const result = SayHi(props) // » <p>Hello, React</p> 

如果是一个类组件,则需要将组件进行实例化,然后调用实例对象的render方法:

  1. // 你的代码  
  2. class SayHi extends React.Component {  
  3.     render() {  
  4.         return <p>Hello, React</p>  
  5.     }  
  6. }  
  7. // React内部  
  8. const instance = new SayHi(props) // » SayHi {}  
  9. const result = instance.render() // » <p>Hello, React</p> 

获取渲染的值

首先给出一个示例

函数组件对应如下:

  1. function ProfilePage(props) { 
  2.   const showMessage = () => { 
  3.     alert('Followed ' + props.user); 
  4.   } 
  5.  
  6.   const handleClick = () => { 
  7.     setTimeout(showMessage, 3000); 
  8.   } 
  9.  
  10.   return ( 
  11.     <button onClick={handleClick}>Follow</button> 
  12.   ) 

类组件对应如下:

  1. class ProfilePage extends React.Component { 
  2.   showMessage() { 
  3.     alert('Followed ' + this.props.user); 
  4.   } 
  5.  
  6.   handleClick() { 
  7.     setTimeout(this.showMessage.bind(this), 3000); 
  8.   } 
  9.  
  10.   render() { 
  11.     return <button onClick={this.handleClick.bind(this)}>Follow</button> 
  12.   } 

两者看起来实现功能是一致的,但是在类组件中,输出this.props.user,Props在 React中是不可变的所以它永远不会改变,但是 this 总是可变的,以便您可以在 render 和生命周期函数中读取新版本

因此,如果我们的组件在请求运行时更新。this.props 将会改变。showMessage方法从“最新”的 props 中读取 user

而函数组件,本身就不存在this,props并不发生改变,因此同样是点击,alert的内容仍旧是之前的内容

小结

两种组件都有各自的优缺点

函数组件语法更短、更简单,这使得它更容易开发、理解和测试

 

而类组件也会因大量使用 this而让人感到困惑

 

责任编辑:武晓燕 来源: JS每日一题
相关推荐

2021-07-12 08:35:24

组件应用场景

2021-06-30 07:19:36

React事件机制

2021-07-07 08:36:45

React应用场景

2021-07-13 07:52:03

ReactHooks组件

2021-07-09 08:33:35

React组件受控

2021-07-29 07:55:20

React Fiber架构引擎

2021-09-13 09:23:52

TypeScript命名空间

2021-08-13 07:56:13

Git pullGit fetch仓库里

2021-06-03 08:14:01

NodeProcessJavaScript

2021-07-02 07:06:20

React组件方式

2021-12-23 07:11:31

开发

2021-05-31 10:35:34

TCPWebSocket协议

2023-12-13 13:31:00

useEffect对象浏览器

2021-06-04 07:55:30

Node Fs 操作

2024-04-03 15:33:04

JWTSession传输信息

2023-02-17 08:10:24

2021-06-08 08:33:23

NodeStream数据

2021-06-07 09:41:48

NodeBuffer 网络协议

2021-07-05 11:06:11

组件React通信

2021-10-29 09:40:21

设计模式软件
点赞
收藏

51CTO技术栈公众号