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

react render props模式實(shí)現(xiàn)組件復(fù)用示例

 更新時(shí)間:2022年07月22日 09:25:24   作者:梨輕巧  
本文主要介紹了react render props模式實(shí)現(xiàn)組件復(fù)用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一 render props的使用步驟

1 創(chuàng)建要復(fù)用的組件,在組件中提供要復(fù)用的狀態(tài)邏輯代碼
2 將要復(fù)用的state作為方法的參數(shù),暴露到組件外部

import React from "react";
import ReactDOM from "react-dom";
 
class App extends React.Component {
 
    render() {
        return <Mouse show={(mouse)=><p>鼠標(biāo)所在位置:{mouse.x},{mouse.y}</p>}/>
    }
 
}
 
//1 創(chuàng)建要復(fù)用的組件,在組件中提供要復(fù)用的狀態(tài)邏輯代碼
class Mouse extends React.Component {
 
    state = {
        x: 0,
        y: 0
    }
 
    //監(jiān)聽鼠標(biāo)移動(dòng)時(shí)間
    componentDidMount() {
        window.addEventListener("mousemove", this.handleMouseMove)
    }
 
    //鼠標(biāo)移動(dòng)的事件處理
    handleMouseMove = e => {
        this.setState({
            x: e.clientX,
            y: e.clientY
        })
    }
 
    render() {
        //2 將要復(fù)用的state作為方法的參數(shù),暴露到組件外部
        return this.props.show(this.state)
    }
}
 
ReactDOM.render(<App/>, document.getElementById("root"));

效果

二 組件的復(fù)用

實(shí)現(xiàn)鼠標(biāo)移動(dòng),圖片移動(dòng)

import imgage from "./images/cat2.gif"
 
class App extends React.Component {
 
    render() {
        return <Mouse show={mouse => {
            return <img src={imgage} alt='貓' style={{
                position: 'absolute',
                // 為了讓鼠標(biāo)在圖片的中間,top減掉了圖片的一半高度,left減掉了圖片一半的寬度
                top: mouse.y-198,
                left: mouse.x-250
            }}/>
        }}></Mouse>
    }
}

效果:圖片跟著鼠標(biāo)走

完整代碼

import React from "react";
import ReactDOM from "react-dom";
import imgage from "./images/cat2.gif"
 
class App extends React.Component {
 
    render() {
        return <Mouse show={mouse => {
            return <img src={imgage} alt='貓' style={{
                position: 'absolute',
                // 為了讓鼠標(biāo)在圖片的中間,top減掉了圖片的一半高度,left減掉了圖片一半的寬度
                top: mouse.y-198,
                left: mouse.x-250
            }}/>
        }}></Mouse>
    }
}
 
//1 創(chuàng)建要復(fù)用的組件,在組件中提供要復(fù)用的狀態(tài)邏輯代碼
class Mouse extends React.Component {
 
    state = {
        x: 0,
        y: 0
    }
 
    //監(jiān)聽鼠標(biāo)移動(dòng)時(shí)間
    componentDidMount() {
        window.addEventListener("mousemove", this.handleMouseMove)
    }
 
    //鼠標(biāo)移動(dòng)的事件處理
    handleMouseMove = e => {
        this.setState({
            x: e.clientX,
            y: e.clientY
        })
    }
 
    render() {
        //2 將要復(fù)用的state作為方法的參數(shù),暴露到組件外部
        return this.props.show(this.state)
    }
}
 
ReactDOM.render(<App/>, document.getElementById("root")
);

三 使用children名代替屬性

 代碼

import React from "react";
import ReactDOM from "react-dom";
import imgage from "./images/cat2.gif"
 
class App extends React.Component {
 
    render() {
        return <Mouse>
            {mouse => {
                return <img src={imgage} alt='貓' style={{
                    position: 'absolute',
                    // 為了讓鼠標(biāo)在圖片的中間,top減掉了圖片的一半高度,left減掉了圖片一半的寬度
                    top: mouse.y - 198,
                    left: mouse.x - 250
                }}/>
            }}
        </Mouse>
    }
}
 
//1 創(chuàng)建要復(fù)用的組件,在組件中提供要復(fù)用的狀態(tài)邏輯代碼
class Mouse extends React.Component {
 
    state = {
        x: 0,
        y: 0
    }
 
    //監(jiān)聽鼠標(biāo)移動(dòng)時(shí)間
    componentDidMount() {
        window.addEventListener("mousemove", this.handleMouseMove)
    }
 
    //鼠標(biāo)移動(dòng)的事件處理
    handleMouseMove = e => {
        this.setState({
            x: e.clientX,
            y: e.clientY
        })
    }
 
    render() {
        //2 將要復(fù)用的state作為方法的參數(shù),暴露到組件外部
        return this.props.children(this.state)
    }
}
 
ReactDOM.render(<App/>, document.getElementById("root")
);

 到此這篇關(guān)于react render props模式實(shí)現(xiàn)組件復(fù)用示例的文章就介紹到這了,更多相關(guān)react render props組件復(fù)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React Native之prop-types進(jìn)行屬性確認(rèn)詳解

    React Native之prop-types進(jìn)行屬性確認(rèn)詳解

    本篇文章主要介紹了React Native之prop-types進(jìn)行屬性確認(rèn)詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • React項(xiàng)目配置prettier和eslint的方法

    React項(xiàng)目配置prettier和eslint的方法

    這篇文章主要介紹了React項(xiàng)目配置prettier和eslint的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • 詳解基于React.js和Node.js的SSR實(shí)現(xiàn)方案

    詳解基于React.js和Node.js的SSR實(shí)現(xiàn)方案

    這篇文章主要介紹了詳解基于React.js和Node.js的SSR實(shí)現(xiàn)方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • GraphQL在react中的應(yīng)用示例詳解

    GraphQL在react中的應(yīng)用示例詳解

    這篇文章主要為大家介紹了GraphQL在react中的應(yīng)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • React18+TS通用后臺(tái)管理系統(tǒng)解決方案落地實(shí)戰(zhàn)示例

    React18+TS通用后臺(tái)管理系統(tǒng)解決方案落地實(shí)戰(zhàn)示例

    這篇文章主要為大家介紹了React18+TS通用后臺(tái)管理系統(tǒng)解決方案落地實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • React項(xiàng)目中不需要jQuery原因分析

    React項(xiàng)目中不需要jQuery原因分析

    在Web開發(fā)的早期,jQuery是一個(gè)革命性的庫(kù),它極大地簡(jiǎn)化了DOM操作、事件處理、動(dòng)畫制作以及Ajax請(qǐng)求等任務(wù),React的出現(xiàn),jQuery在新項(xiàng)目中的必要性開始受到質(zhì)疑,本文將探討為什么在React應(yīng)用中不需要jQuery,感興趣的朋友可以參考下
    2024-02-02
  • react-beautiful-dnd 實(shí)現(xiàn)組件拖拽功能

    react-beautiful-dnd 實(shí)現(xiàn)組件拖拽功能

    這篇文章主要介紹了react-beautiful-dnd 實(shí)現(xiàn)組件拖拽功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • 解決react組件渲染兩次的問(wèn)題

    解決react組件渲染兩次的問(wèn)題

    這篇文章主要介紹了解決react組件渲染兩次的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • react 創(chuàng)建單例組件的方法

    react 創(chuàng)建單例組件的方法

    這篇文章主要介紹了react 創(chuàng)建單例組件的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • React項(xiàng)目搭建與Echarts工具使用詳解

    React項(xiàng)目搭建與Echarts工具使用詳解

    這篇文章主要介紹了React項(xiàng)目搭建與Echarts工具使用詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03

最新評(píng)論