自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

面試官:說說對React中類組件和函數(shù)組件的理解?有什么區(qū)別?

開發(fā) 前端
類組件,顧名思義,也就是通過使用ES6類的編寫形式去編寫組件,該類必須繼承React.Component。

[[410052]]

本文轉(zhuǎn)載自微信公眾號「JS每日一題」,作者灰灰。轉(zhuǎn)載本文請聯(lián)系JS每日一題公眾號。

一、類組件

類組件,顧名思義,也就是通過使用ES6類的編寫形式去編寫組件,該類必須繼承React.Component

如果想要訪問父組件傳遞過來的參數(shù),可通過this.props的方式去訪問

在組件中必須實現(xiàn)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.   } 

二、函數(shù)組件

函數(shù)組件,顧名思義,就是通過函數(shù)編寫的形式去實現(xiàn)一個React組件,是React中定義組件最簡單的方式

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

函數(shù)第一個參數(shù)為props用于接收父組件傳遞過來的參數(shù)

三、區(qū)別

針對兩種React組件,其區(qū)別主要分成以下幾大方向:

  • 編寫形式
  • 狀態(tài)管理
  • 生命周期
  • 調(diào)用方式
  • 獲取渲染的值

編寫形式

兩者最明顯的區(qū)別在于編寫形式的不同,同一種功能的實現(xiàn)可以分別對應(yīng)類組件和函數(shù)組件的編寫形式

函數(shù)組件:

  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.   } 

狀態(tài)管理

在hooks出來之前,函數(shù)組件就是無狀態(tài)組件,不能保管組件的狀態(tài),不像類組件中調(diào)用setState

如果想要管理state狀態(tài),可以使用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情況下,一般如果函數(shù)組件調(diào)用state,則需要創(chuàng)建一個類組件或者state提升到你的父組件中,然后通過props對象傳遞到子組件

生命周期

在函數(shù)組件中,并不存在生命周期,這是因為這些生命周期鉤子都來自于繼承的React.Component

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

但是函數(shù)組件使用useEffect也能夠完成替代生命周期的作用,這里給出一個簡單的例子:

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

上述簡單的例子對應(yīng)類組件中的componentDidMount生命周期

如果在useEffect回調(diào)函數(shù)中return一個函數(shù),則return函數(shù)會在組件卸載的時候執(zhí)行,正如componentWillUnmount

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

調(diào)用方式

如果是一個函數(shù)組件,調(diào)用則是執(zhí)行函數(shù)即可:

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

如果是一個類組件,則需要將組件進行實例化,然后調(diào)用實例對象的render方法:

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

獲取渲染的值

首先給出一個示例

函數(shù)組件對應(yīng)如下:

  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.   ) 

類組件對應(yīng)如下:

  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.   } 

兩者看起來實現(xiàn)功能是一致的,但是在類組件中,輸出this.props.user,Props在 React中是不可變的所以它永遠(yuǎn)不會改變,但是 this 總是可變的,以便您可以在 render 和生命周期函數(shù)中讀取新版本

因此,如果我們的組件在請求運行時更新。this.props 將會改變。showMessage方法從“最新”的 props 中讀取 user

而函數(shù)組件,本身就不存在this,props并不發(fā)生改變,因此同樣是點擊,alert的內(nèi)容仍舊是之前的內(nèi)容

小結(jié)

兩種組件都有各自的優(yōu)缺點

函數(shù)組件語法更短、更簡單,這使得它更容易開發(fā)、理解和測試

 

而類組件也會因大量使用 this而讓人感到困惑

 

責(zé)任編輯:武曉燕 來源: JS每日一題
相關(guān)推薦

2021-06-30 07:19:36

React事件機制

2021-07-12 08:35:24

組件應(yīng)用場景

2021-07-07 08:36:45

React應(yīng)用場景

2021-07-13 07:52:03

ReactHooks組件

2021-07-29 07:55:20

React Fiber架構(gòu)引擎

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-09 08:33:35

React組件受控

2021-05-31 10:35:34

TCPWebSocket協(xié)議

2021-07-02 07:06:20

React組件方式

2021-12-23 07:11:31

開發(fā)

2023-12-13 13:31:00

useEffect對象瀏覽器

2024-05-30 08:04:20

Netty核心組件架構(gòu)

2021-06-04 07:55:30

Node Fs 操作

2024-04-03 15:33:04

JWTSession傳輸信息

2024-09-19 08:42:43

2021-06-07 09:41:48

NodeBuffer 網(wǎng)絡(luò)協(xié)議

2021-06-08 08:33:23

NodeStream數(shù)據(jù)

2023-02-17 08:10:24

點贊
收藏

51CTO技術(shù)棧公眾號