最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React進階學習之組件的解耦之道

 更新時間:2017年08月07日 10:05:10   作者:楓上霧棋  
這篇文章主要給大家介紹了關(guān)于React進階之組件的解耦之道,文中通過詳細的示例代碼給大家介紹了組件分割與解耦的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧。

前言

眾所周知,React中的組件非常的靈活可擴展,不過隨著業(yè)務(wù)復(fù)雜度的增加和許多外部工具庫的引入,組件往往也會顯得浮腫,接下來我們就一起來看看常見的幾種,遵循單一職責原則的,組件分割與解耦的方法,話不多說了,來一起看看詳細的介紹:

一、分割 render 函數(shù)

當一個組件渲染的內(nèi)容較多時,有一個快速并且通用的方法是創(chuàng)建sub-render函數(shù)來簡化原來龐大的 render

class Panel extends React.Component {
 renderHeading() {
 // ...
 }

 renderBody() {
 // ...
 }

 render() {
 return (
 <div>
 {this.renderHeading()}
 {this.renderBody()}
 </div>
 );
 }
}

為了再次簡化sub-render函數(shù),我們還可以采用Functional Components寫法,這種方式生成了更小的處理單元,且更有利于測試

const PanelHeader = (props) => (
 // ...
);

const PanelBody = (props) => (
 // ...
);

class Panel extends React.Component {
 render() {
 return (
 <div>
 // Nice and explicit about which props are used
 <PanelHeader title={this.props.title}/>
 <PanelBody content={this.props.content}/>
 </div>
 );
 }
}

二、用 props 傳遞元素

如果一個組件的狀態(tài)或配置較多,我們可以運用props傳遞元素而不僅是數(shù)據(jù),比如再聲明一個組件,使其中的父組件只專注于配置

class CommentTemplate extends React.Component {
 static propTypes = {
 // Declare slots as type node
 metadata: PropTypes.node,
 actions: PropTypes.node,
 };

 render() {
 return (
 <div>
 <CommentHeading>
  <Avatar user={...}/>

  // Slot for metadata
  <span>{this.props.metadata}</span>

 </CommentHeading>
 <CommentBody/>
 <CommentFooter>
  <Timestamp time={...}/>

  // Slot for actions
  <span>{this.props.actions}</span>

 </CommentFooter>
 </div>
 );
 }
}

父組件

class Comment extends React.Component {
 render() {
 const metadata = this.props.publishTime ?
 <PublishTime time={this.props.publishTime} /> :
 <span>Saving...</span>;

 const actions = [];
 if (this.props.isSignedIn) {
 actions.push(<LikeAction />);
 actions.push(<ReplyAction />);
 }
 if (this.props.isAuthor) {
 actions.push(<DeleteAction />);
 }

 return <CommentTemplate metadata={metadata} actions={actions} />;
 }
}

三、使用高階組件

實現(xiàn)點擊某組件的超鏈接,發(fā)送該組件的 ID,我們大多的解決方法可能如下

class Document extends React.Component {
 componentDidMount() {
 ReactDOM.findDOMNode(this).addEventListener('click', this.onClick);
 }

 componentWillUnmount() {
 ReactDOM.findDOMNode(this).removeEventListener('click', this.onClick);
 }

 onClick = (e) => {
 if (e.target.tagName === 'A') { // Naive check for <a> elements
 sendAnalytics('link clicked', {
 documentId: this.props.documentId // Specific information to be sent
 });
 }
 };

 render() {
 // ...
 }
}

然而它卻存在代碼不能復(fù)用,組件重構(gòu)困難等問題

我們可以使用高階組件來解決這些問題,顧名思義,高階組件就是一個函數(shù),傳給它一個組件,它返回一個新的組件

function withLinkAnalytics(mapPropsToData, WrappedComponent) {
 class LinkAnalyticsWrapper extends React.Component {
 componentDidMount() {
 ReactDOM.findDOMNode(this).addEventListener('click', this.onClick);
 }

 componentWillUnmount() {
 ReactDOM.findDOMNode(this).removeEventListener('click', this.onClick);
 }

 onClick = (e) => {
 if (e.target.tagName === 'A') { // Naive check for <a> elements
 const data = mapPropsToData ? mapPropsToData(this.props) : {};
 sendAnalytics('link clicked', data);
 }
 };

 render() {
 // Simply render the WrappedComponent with all props
 return <WrappedComponent {...this.props} />;
 }
 }

 return LinkAnalyticsWrapper;
}

簡化代碼如下

class Document extends React.Component {
 render() {
 // ...
 }
}

export default withLinkAnalytics((props) => ({
 documentId: props.documentId
}), Document);

總結(jié)

以上 3 個 React 組件的解耦重構(gòu)方法都可以直接拿來運用,最開始可能會覺得有點棘手,但是沒關(guān)系,只要堅持下來,你就會寫出更強壯和可復(fù)用的代碼。

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

原文鏈接: Techniques for decomposing React components

相關(guān)文章

  • 基于React實現(xiàn)下拉刷新效果

    基于React實現(xiàn)下拉刷新效果

    這篇文章主要介紹了如何基于react實現(xiàn)下拉刷新效果,在下拉的時候會進入loading狀態(tài),文中有詳細的代碼示例供大家參考,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-03-03
  • 詳解React native fetch遇到的坑

    詳解React native fetch遇到的坑

    這篇文章主要介紹了詳解React native fetch遇到的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • React Diff原理深入分析

    React Diff原理深入分析

    這篇文章主要介紹了React Diff原理的相關(guān)資料,幫助大家更好的理解和學習使用React框架,感興趣的朋友可以了解下
    2021-04-04
  • React?less?實現(xiàn)縱橫柱狀圖示例詳解

    React?less?實現(xiàn)縱橫柱狀圖示例詳解

    這篇文章主要介紹了React?less?實現(xiàn)縱橫柱狀圖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • react學習筆記之state以及setState的使用

    react學習筆記之state以及setState的使用

    這篇文章主要介紹了react學習筆記之state以及setState的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • redux功能強大的Middleware中間件使用學習

    redux功能強大的Middleware中間件使用學習

    這篇文章主要為大家介紹了redux功能強大的Middleware中間件使用學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • React中用@符號編寫文件路徑實現(xiàn)方法介紹

    React中用@符號編寫文件路徑實現(xiàn)方法介紹

    在Vue中,我們導(dǎo)入文件時,文件路徑中可以使用@符號指代src目錄,極大的簡化了我們對路徑的書寫。但是react中,要想實現(xiàn)這種方式書寫文件路徑,需要寫配置文件來實現(xiàn)
    2022-09-09
  • React三大屬性之props的使用詳解

    React三大屬性之props的使用詳解

    這篇文章主要介紹了React三大屬性之props的使用詳解,幫助大家更好的理解和學習使用React,感興趣的朋友可以了解下
    2021-04-04
  • react用Redux中央倉庫實現(xiàn)一個todolist

    react用Redux中央倉庫實現(xiàn)一個todolist

    這篇文章主要為大家詳細介紹了react用Redux中央倉庫實現(xiàn)一個todolist,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • react中使用swiper的具體方法

    react中使用swiper的具體方法

    本篇文章主要介紹了react中使用swiper的具體方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05

最新評論

清流县| 翁牛特旗| 嘉祥县| 化州市| 蓬溪县| 玉屏| 宁安市| 佛冈县| 邵武市| 连平县| 锦屏县| 千阳县| 双牌县| 儋州市| 进贤县| 化州市| 天等县| 山东| 孝昌县| 长阳| 卓资县| 明星| 友谊县| 军事| 淅川县| 宁国市| 沽源县| 图片| 宁强县| 同心县| 朝阳区| 且末县| 漾濞| 宣威市| 木兰县| 温宿县| 额敏县| 招远市| 定襄县| 桂林市| 田东县|