深入理解react 組件類型及使用場(chǎng)景
函數(shù)組件 vs 類組件
函數(shù)組件(Functional Component )和類組件(Class Component),劃分依據(jù)是根據(jù)組件的定義方式。函數(shù)組件使用函數(shù)定義組件,類組件使用ES6 class定義組件
// 函數(shù)組件
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
// 類組件
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
- 函數(shù)組件的寫法要比類組件簡潔,不過類組件比函數(shù)組件功能更加強(qiáng)大。函數(shù)組件更加專注和單一,承擔(dān)的職責(zé)也更加清晰,它只是一個(gè)返回React 元素的函數(shù),只關(guān)注對(duì)應(yīng)UI的展現(xiàn)。函數(shù)組件接收外部傳入的props,返回對(duì)應(yīng)UI的DOM描述,
- 類組件可以維護(hù)自身的狀態(tài)變量,即組件的state,類組件還有不同的生命周期方法,可以讓開發(fā)者能夠在組件的不同階段(掛載、更新、卸載),對(duì)組件做更多的控制。
無狀態(tài)組件 vs 有狀態(tài)組件
函數(shù)組件一定屬于無狀態(tài)組件 (劃分依據(jù)是根據(jù)組件內(nèi)部是否維護(hù)state)
// 無狀態(tài)組件
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
// 有狀態(tài)類組件
class Welcome extends React.Component {
constructor(props) {
super(props);
this.state = {
show: true
}
}
render() {
const { show } = this.state;
return (
<>
{ show && <h1>Hello, {this.props.name}</h1> }
</>
)
}
}
展示型組件 vs 容器型組件
展示型組件(Presentational Component)和容器型組件(Container Component),劃分依據(jù)是根據(jù)組件的職責(zé)。 (展示型組件一般是無狀態(tài)組件,不需要state)
class UserListContainer extends React.Component{
constructor(props){
super(props);
this.state = {
users: []
}
}
componentDidMount() {
var that = this;
fetch('/path/to/user-api').then(function(response) {
response.json().then(function(data) {
that.setState({users: data})
});
});
}
render() {
return (
<UserList users={this.state.users} />
)
}
}
function UserList(props) {
return (
<div>
<ul className="user-list">
{props.users.map(function(user) {
return (
<li key={user.id}>
<span>{user.name}</span>
</li>
);
})}
</ul>
</div>
)
}
展示型組件和容器型組件是可以互相嵌套的,展示型組件的子組件既可以包含展示型組件,也可以包含容器型組件,容器型組件也是如此。例如,當(dāng)一個(gè)容器型組件承擔(dān)的數(shù)據(jù)管理工作過于復(fù)雜時(shí),可以在它的子組件中定義新的容器型組件,由新組件分擔(dān)數(shù)據(jù)的管理。展示型組件和容器型組件的劃分完全取決于組件所做的事情。
總結(jié)
通過上面的介紹,可以發(fā)現(xiàn)這三組概念有很多重疊部分。這三組概念都體現(xiàn)了關(guān)注點(diǎn)分離的思想:UI展現(xiàn)和數(shù)據(jù)邏輯的分離。函數(shù)組件、無狀態(tài)組件和展示型組件主要關(guān)注UI展現(xiàn),類組件、有狀態(tài)組件和容器型組件主要關(guān)注數(shù)據(jù)邏輯。但由于它們的劃分依據(jù)不同,它們并非完全等價(jià)的概念。它們之間的關(guān)聯(lián)關(guān)系可以歸納為:函數(shù)組件一定是無狀態(tài)組件,展示型組件一般是無狀態(tài)組件;類組件既可以是有狀態(tài)組件,又可以是無狀態(tài)組件,容器型組件一般是有狀態(tài)組件。
舉個(gè)🌰
import React, { Component } from 'react'
import { observer } from 'mobx-react'
import { Switch } from 'antd'
@observer
class BaseInfoView extends Component {
constructor(props) {
super(props)
}
render() {
const {
ideaName,
resourceLocationDto,
switchState,
slotId
} = this.props.model
return (
<div>
<div className="adx-form-item-title">基本信息</div>
<div className="ant-form-horizontal">
<div className="ant-form-item region">
<label className="ant-col-4 ant-form-item-label">
<span className="require-tip">*</span>創(chuàng)意名稱:
</label>
<div className="ant-col-19 ml10 region-input">
<div className="ant-form-text">
{ ideaName }
</div>
</div>
</div>
<div className="ant-form-item region">
<label className="ant-col-4 ant-form-item-label">
<span className="require-tip">*</span>所屬資源位:
</label>
<div className="ant-col-19 ml10 region-input">
<div className="ant-form-text">
{ resourceLocationDto && resourceLocationDto.resourceName }
</div>
</div>
</div>
<div className="ant-form-item region">
<label className="ant-col-4 ant-form-item-label">
<span className="require-tip">*</span>創(chuàng)意狀態(tài):
</label>
<div className="ant-col-19 ml10 region-input">
<div className="ant-form-text">
<Switch checked={switchState == 1}/>
</div>
</div>
</div>
<div className="ant-form-item region">
<label className="ant-col-4 ant-form-item-label">
<span className="require-tip">*</span>推啊廣告位ID:
</label>
<div className="ant-col-19 ml10 region-input">
<div className="ant-form-text">
{ slotId }
</div>
</div>
</div>
</div>
</div>
)
}
}
export default BaseInfoView
完全可以寫成函數(shù)組件
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
聊一聊我對(duì) React Context 的理解以及應(yīng)用
這篇文章主要介紹了聊一聊我對(duì) React Context 的理解以及應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
React Native使用fetch實(shí)現(xiàn)圖片上傳的示例代碼
本篇文章主要介紹了React Native使用fetch實(shí)現(xiàn)圖片上傳的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
在react-antd中彈出層form內(nèi)容傳遞給父組件的操作
這篇文章主要介紹了在react-antd中彈出層form內(nèi)容傳遞給父組件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
React+Node.js實(shí)現(xiàn)大文件傳輸與斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了如何使用React前端和Node.js后端實(shí)現(xiàn)大文件傳輸和斷點(diǎn)續(xù)傳的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考下2024-12-12
React+Ant Design開發(fā)環(huán)境搭建的實(shí)現(xiàn)步驟
這篇文章主要介紹了React+Ant Design開發(fā)環(huán)境搭建的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

