做爰高潮a片〈毛片〉,尤物av天堂一区二区在线观看,一本久久A久久精品VR综合,添女人荫蒂全部过程av

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

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

來源:懂視網 責編:小采 時間:2020-11-27 22:33:17
文檔

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

React進階學習之組件的解耦之道:前言 眾所周知,React中的組件非常的靈活可擴展,不過隨著業務復雜度的增加和許多外部工具庫的引入,組件往往也會顯得浮腫,接下來我們就一起來看看常見的幾種,遵循單一職責原則的,組件分割與解耦的方法,話不多說了,來一起看看詳細的介紹: 一、分割 r
推薦度:
導讀React進階學習之組件的解耦之道:前言 眾所周知,React中的組件非常的靈活可擴展,不過隨著業務復雜度的增加和許多外部工具庫的引入,組件往往也會顯得浮腫,接下來我們就一起來看看常見的幾種,遵循單一職責原則的,組件分割與解耦的方法,話不多說了,來一起看看詳細的介紹: 一、分割 r

前言

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

一、分割 render 函數

當一個組件渲染的內容較多時,有一個快速并且通用的方法是創建sub-render函數來簡化原來龐大的 render

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

 renderBody() {
 // ...
 }

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

為了再次簡化sub-render函數,我們還可以采用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 傳遞元素

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

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} />;
 }
}

三、使用高階組件

實現點擊某組件的超鏈接,發送該組件的 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() {
 // ...
 }
}

然而它卻存在代碼不能復用,組件重構困難等問題

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

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

總結

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

好了,

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

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

React進階學習之組件的解耦之道:前言 眾所周知,React中的組件非常的靈活可擴展,不過隨著業務復雜度的增加和許多外部工具庫的引入,組件往往也會顯得浮腫,接下來我們就一起來看看常見的幾種,遵循單一職責原則的,組件分割與解耦的方法,話不多說了,來一起看看詳細的介紹: 一、分割 r
推薦度:
標簽: 學習 組件 React
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 名山县| 莱阳市| 昌平区| 石家庄市| 麻城市| 永济市| 樟树市| 鹤山市| 镇坪县| 台北县| 霍州市| 鸡西市| 册亨县| 思南县| 阳城县| 石河子市| 杭锦旗| 昂仁县| 华阴市| 东海县| 宁城县| 旬邑县| 赤壁市| 石柱| 西贡区| 弥渡县| 桃江县| 洪泽县| 宕昌县| 宁海县| 和田市| 沙洋县| 彰武县| 自贡市| 莆田市| 电白县| 措勤县| 哈巴河县| 呈贡县| 巴林右旗| 九江市|