面試官:說說對(duì)React中類組件和函數(shù)組件的理解?有什么區(qū)別?
本文轉(zhuǎn)載自微信公眾號(hào)「JS每日一題」,作者灰灰。轉(zhuǎn)載本文請(qǐng)聯(lián)系JS每日一題公眾號(hào)。
一、類組件
類組件,顧名思義,也就是通過使用ES6類的編寫形式去編寫組件,該類必須繼承React.Component
如果想要訪問父組件傳遞過來的參數(shù),可通過this.props的方式去訪問
在組件中必須實(shí)現(xiàn)render方法,在return中返回React對(duì)象,如下:
- class Welcome extends React.Component {
- constructor(props) {
- super(props)
- }
- render() {
- return <h1>Hello, {this.props.name}</h1>
- }
- }
二、函數(shù)組件
函數(shù)組件,顧名思義,就是通過函數(shù)編寫的形式去實(shí)現(xiàn)一個(gè)React組件,是React中定義組件最簡(jiǎn)單的方式
- function Welcome(props) {
- return <h1>Hello, {props.name}</h1>;
- }
函數(shù)第一個(gè)參數(shù)為props用于接收父組件傳遞過來的參數(shù)
三、區(qū)別
針對(duì)兩種React組件,其區(qū)別主要分成以下幾大方向:
- 編寫形式
- 狀態(tài)管理
- 生命周期
- 調(diào)用方式
- 獲取渲染的值
編寫形式
兩者最明顯的區(qū)別在于編寫形式的不同,同一種功能的實(shí)現(xiàn)可以分別對(duì)應(yīng)類組件和函數(shù)組件的編寫形式
函數(shù)組件:
- function Welcome(props) {
- return <h1>Hello, {props.name}</h1>;
- }
類組件:
- cass Welcome extends React.Component {
- constructor(props) {
- super(props)
- }
- render() {
- return <h1>Hello, {this.props.name}</h1>
- }
- }
狀態(tài)管理
在hooks出來之前,函數(shù)組件就是無狀態(tài)組件,不能保管組件的狀態(tài),不像類組件中調(diào)用setState
如果想要管理state狀態(tài),可以使用useState,如下:
- const FunctionalComponent = () => {
- const [count, setCount] = React.useState(0);
- return (
- <div>
- <p>count: {count}</p>
- <button onClick={() => setCount(count + 1)}>Click</button>
- </div>
- );
- };
在使用hooks情況下,一般如果函數(shù)組件調(diào)用state,則需要?jiǎng)?chuàng)建一個(gè)類組件或者state提升到你的父組件中,然后通過props對(duì)象傳遞到子組件
生命周期
在函數(shù)組件中,并不存在生命周期,這是因?yàn)檫@些生命周期鉤子都來自于繼承的React.Component
所以,如果用到生命周期,就只能使用類組件
但是函數(shù)組件使用useEffect也能夠完成替代生命周期的作用,這里給出一個(gè)簡(jiǎn)單的例子:
- const FunctionalComponent = () => {
- useEffect(() => {
- console.log("Hello");
- }, []);
- return <h1>Hello, World</h1>;
- };
上述簡(jiǎn)單的例子對(duì)應(yīng)類組件中的componentDidMount生命周期
如果在useEffect回調(diào)函數(shù)中return一個(gè)函數(shù),則return函數(shù)會(huì)在組件卸載的時(shí)候執(zhí)行,正如componentWillUnmount
- const FunctionalComponent = () => {
- React.useEffect(() => {
- return () => {
- console.log("Bye");
- };
- }, []);
- return <h1>Bye, World</h1>;
- };
調(diào)用方式
如果是一個(gè)函數(shù)組件,調(diào)用則是執(zhí)行函數(shù)即可:
- // 你的代碼
- function SayHi() {
- return <p>Hello, React</p>
- }
- // React內(nèi)部
- const result = SayHi(props) // » <p>Hello, React</p>
如果是一個(gè)類組件,則需要將組件進(jìn)行實(shí)例化,然后調(diào)用實(shí)例對(duì)象的render方法:
- // 你的代碼
- class SayHi extends React.Component {
- render() {
- return <p>Hello, React</p>
- }
- }
- // React內(nèi)部
- const instance = new SayHi(props) // » SayHi {}
- const result = instance.render() // » <p>Hello, React</p>
獲取渲染的值
首先給出一個(gè)示例
函數(shù)組件對(duì)應(yīng)如下:
- function ProfilePage(props) {
- const showMessage = () => {
- alert('Followed ' + props.user);
- }
- const handleClick = () => {
- setTimeout(showMessage, 3000);
- }
- return (
- <button onClick={handleClick}>Follow</button>
- )
- }
類組件對(duì)應(yīng)如下:
- class ProfilePage extends React.Component {
- showMessage() {
- alert('Followed ' + this.props.user);
- }
- handleClick() {
- setTimeout(this.showMessage.bind(this), 3000);
- }
- render() {
- return <button onClick={this.handleClick.bind(this)}>Follow</button>
- }
- }
兩者看起來實(shí)現(xiàn)功能是一致的,但是在類組件中,輸出this.props.user,Props在 React中是不可變的所以它永遠(yuǎn)不會(huì)改變,但是 this 總是可變的,以便您可以在 render 和生命周期函數(shù)中讀取新版本
因此,如果我們的組件在請(qǐng)求運(yùn)行時(shí)更新。this.props 將會(huì)改變。showMessage方法從“最新”的 props 中讀取 user
而函數(shù)組件,本身就不存在this,props并不發(fā)生改變,因此同樣是點(diǎn)擊,alert的內(nèi)容仍舊是之前的內(nèi)容
小結(jié)
兩種組件都有各自的優(yōu)缺點(diǎn)
函數(shù)組件語法更短、更簡(jiǎn)單,這使得它更容易開發(fā)、理解和測(cè)試
而類組件也會(huì)因大量使用 this而讓人感到困惑