React高階組件優(yōu)化文件結(jié)構(gòu)流程詳解
其實(shí)高階組件是一個(gè)將組件寫(xiě)的更靈活的方式,他的應(yīng)用場(chǎng)景在業(yè)務(wù)開(kāi)發(fā)中會(huì)非常多樣
這里 我們演示一種 主要還是解決問(wèn)題的思想最重要 或者是 這個(gè)不叫解決問(wèn)題 而是設(shè)計(jì)組件結(jié)構(gòu)的思路
我們來(lái)模擬一個(gè)場(chǎng)景
在src下有一個(gè) components 文件夾目錄
在 components 下有一個(gè) banner.jsx組件 參考代碼如下
import React,{ Component } from 'react'; class Banner extends Component { constructor() { super(); this.state = { loading: true, banner: null } } componentDidMount() { fetch("http://iwenwiki.com/api/blueberrypai/getIndexBanner.php").then(res => res.json()).then(banner => { this.setState({ loading: false, banner: banner }) }) } render() { if(this.state.loading) { return ( <div>loading</div> ) } else { return ( <h1> {this.state.banner.banner[0].title }</h1> ) } } } export default Banner;
然后 在components下有一個(gè) chengpin.jsx組件 參考代碼如下
import React,{ Component } from 'react'; class Chengpin extends Component { constructor() { super(); this.state = { loading: true, chengpin: null } } componentDidMount() { fetch("http://iwenwiki.com/api/blueberrypai/getChengpinInfo.php").then(res => res.json()).then(chengpin => { this.setState({ loading: false, chengpin: chengpin }) }) } render() { if(this.state.loading) { return ( <div>loading</div> ) }else{ return ( <h1> {this.state.chengpin.chengpinInfo[0].title}</h1> ) } } } export default Chengpin;
其實(shí) 兩個(gè)組件寫(xiě)的寫(xiě)法 基本就是一樣的
只是調(diào)用的接口 和使用的字段不一樣而已 像這么相似的東西 我們真的沒(méi)必要寫(xiě)兩個(gè)組件來(lái)處理 直接一個(gè)高階組件就行了
我們先將這兩個(gè)組件刪了
然后 我們?cè)赾omponents目錄下創(chuàng)建 withFetch.jsx組件
參考代碼如下
import React from 'react' const withFetch = (url) => (View) => { return class extends Component { constructor() { super(); this.state = { loading: true, data: null } } componentDidMount() { fetch(url) .then(res => res.json()) then(data => { this.setstate({ loading: false, data: data }); }) } render() { if(this.state.loading) { return( <div>loadding....</div> ) }else{ return <View data={this.state.data }></View> } } } } export default withFetch;
這樣 我們的一個(gè)高階組件就寫(xiě)好了 然后 講解一下
我們 方法 第一個(gè)參數(shù) 接收一個(gè)url參數(shù) 這個(gè)參數(shù) 用來(lái)控制fetch網(wǎng)絡(luò)請(qǐng)求的地址 也可以理解為控制他去調(diào)哪個(gè)接口
然后 第二個(gè)參數(shù) 是一個(gè)組件
最后 將請(qǐng)求回來(lái)的數(shù)據(jù) 給了組件的data參數(shù)
然后 在components下創(chuàng)建 componentTransfer.jsx文件 用于使用高階組件
import React from "react" import withFetch from "./withFetch" const Chengpin = withFetch("http://iwenwiki.com/api/blueberrypai/getChengpinInfo.php")(props =>{ return( <div> <h1> {props.data.chengpinInfo[0].title}</h1> </div> ) }) const Banner = withFetch("http://iwenwiki.com/api/blueberrypai/getIndexBanner.php")(props =>{ return( <div> <h1>{ props.data.banner[0].title } </h1> </div> ) }) let data = { Chengpin: Chengpin, Banner: Banner } export default data;
這樣 我們就將 url作為了 參數(shù) 然后 第二個(gè)參數(shù) 現(xiàn)寫(xiě)的 代碼結(jié)構(gòu) 通過(guò)接到的 data參數(shù)來(lái)處理組件內(nèi)部 然后再傳給高階組件 去渲染
這個(gè)寫(xiě)法就會(huì)使我們的多組件項(xiàng)目簡(jiǎn)化很多
最后 修改 src目錄下的 App.js 代碼如下
import './App.css'; import React from "react"; import ComponentTransfer from "./components/componentTransfer"; export default class App extends React.Component{ constructor(props){ super(props); this.state = { } } render(){ return ( <div> <ComponentTransfer.Chengpin/> <ComponentTransfer.Banner/> </div> ) } }
運(yùn)行結(jié)果如下
大家可以照著寫(xiě)一次 體會(huì)一下這種關(guān)聯(lián)渲染的邏輯 用好了 可以讓你的項(xiàng)目組件布局簡(jiǎn)便非常多 組件越多發(fā)揮空間越大
到此這篇關(guān)于React高階組件優(yōu)化文件結(jié)構(gòu)流程詳解的文章就介紹到這了,更多相關(guān)React優(yōu)化文件結(jié)構(gòu)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React從插槽、路由、redux的詳細(xì)過(guò)程
React需要自己開(kāi)發(fā)支持插槽功能,原理:父組件組件中寫(xiě)入的HTML,可以傳入子組件的props中,這篇文章主要介紹了React從插槽、路由、redux的詳細(xì)過(guò)程,需要的朋友可以參考下2022-10-10ReactNative 之FlatList使用及踩坑封裝總結(jié)
本篇文章主要介紹了ReactNative 之FlatList使用及踩坑封裝總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11React Native 使用Fetch發(fā)送網(wǎng)絡(luò)請(qǐng)求的示例代碼
本篇文章主要介紹了React Native 使用Fetch發(fā)送網(wǎng)絡(luò)請(qǐng)求的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12IntersectionObserver實(shí)現(xiàn)加載更多組件demo
這篇文章主要為大家介紹了IntersectionObserver實(shí)現(xiàn)加載更多組件demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07React-Native之定時(shí)器Timer的實(shí)現(xiàn)代碼
本篇文章主要介紹了React-Native之定時(shí)器Timer的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10react純函數(shù)組件setState更新頁(yè)面不刷新的解決
在開(kāi)發(fā)過(guò)程中,經(jīng)常遇到組件數(shù)據(jù)無(wú)法更新,本文主要介紹了react純函數(shù)組件setState更新頁(yè)面不刷新的解決,感興趣的可以了解一下2021-06-06