欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React高階組件優(yōu)化文件結(jié)構(gòu)流程詳解

 更新時(shí)間:2023年01月29日 15:46:05   作者:-耿瑞-  
高階組件就是接受一個(gè)組件作為參數(shù)并返回一個(gè)新組件(功能增強(qiáng)的組件)的函數(shù)。這里需要注意高階組件是一個(gè)函數(shù),并不是組件,這一點(diǎn)一定要注意,本文給大家分享React 高階組件HOC使用小結(jié),一起看看吧

其實(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實(shí)現(xiàn)合成事件的源碼分析

    React實(shí)現(xiàn)合成事件的源碼分析

    React?中的事件,是對(duì)原生事件的封裝,叫做合成事件。抽象出一層合成事件,是為了做兼容,抹平不同瀏覽器之間的差異。本文將從事件綁定和事件觸發(fā)角度,帶大家解讀下源碼,感興趣的可以了解一下
    2022-12-12
  • React從插槽、路由、redux的詳細(xì)過(guò)程

    React從插槽、路由、redux的詳細(xì)過(guò)程

    React需要自己開(kāi)發(fā)支持插槽功能,原理:父組件組件中寫(xiě)入的HTML,可以傳入子組件的props中,這篇文章主要介紹了React從插槽、路由、redux的詳細(xì)過(guò)程,需要的朋友可以參考下
    2022-10-10
  • ReactNative 之FlatList使用及踩坑封裝總結(jié)

    ReactNative 之FlatList使用及踩坑封裝總結(jié)

    本篇文章主要介紹了ReactNative 之FlatList使用及踩坑封裝總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • React Native 使用Fetch發(fā)送網(wǎng)絡(luò)請(qǐng)求的示例代碼

    React Native 使用Fetch發(fā)送網(wǎng)絡(luò)請(qǐng)求的示例代碼

    本篇文章主要介紹了React Native 使用Fetch發(fā)送網(wǎng)絡(luò)請(qǐng)求的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • IntersectionObserver實(shí)現(xiàn)加載更多組件demo

    IntersectionObserver實(shí)現(xiàn)加載更多組件demo

    這篇文章主要為大家介紹了IntersectionObserver實(shí)現(xiàn)加載更多組件demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • React路由組件三種傳參方式分析講解

    React路由組件三種傳參方式分析講解

    本文主要介紹了React組件通信之路由傳參(react-router-dom),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • react基礎(chǔ)知識(shí)總結(jié)

    react基礎(chǔ)知識(shí)總結(jié)

    這篇文章主要介紹了react常用的基礎(chǔ)知識(shí)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用react框架,感興趣的朋友可以了解下
    2021-04-04
  • React-Native之定時(shí)器Timer的實(shí)現(xiàn)代碼

    React-Native之定時(shí)器Timer的實(shí)現(xiàn)代碼

    本篇文章主要介紹了React-Native之定時(shí)器Timer的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • React學(xué)習(xí)筆記之條件渲染(一)

    React學(xué)習(xí)筆記之條件渲染(一)

    條件渲染在React里就和js里的條件語(yǔ)句一樣。下面這篇文章主要給大家介紹了關(guān)于React學(xué)習(xí)記錄之條件渲染的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-07-07
  • react純函數(shù)組件setState更新頁(yè)面不刷新的解決

    react純函數(shù)組件setState更新頁(yè)面不刷新的解決

    在開(kāi)發(fā)過(guò)程中,經(jīng)常遇到組件數(shù)據(jù)無(wú)法更新,本文主要介紹了react純函數(shù)組件setState更新頁(yè)面不刷新的解決,感興趣的可以了解一下
    2021-06-06

最新評(píng)論