react render props模式實(shí)現(xiàn)組件復(fù)用示例
一 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)聽(tīng)鼠標(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)聽(tīng)鼠標(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)聽(tīng)鼠標(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類式組件中setState的應(yīng)用
這篇文章主要為大家詳細(xì)介紹了介紹了React類式組件中setState的三種寫法以及簡(jiǎn)單討論下setState?到底是同步的還是異步的,感興趣的可以了解下2024-02-02
詳解React Native頂|底部導(dǎo)航使用小技巧
本篇文章主要介紹了詳解React Native頂|底部導(dǎo)航使用小技巧 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
react-redux的connect與React.forwardRef結(jié)合ref失效的解決
這篇文章主要介紹了react-redux的connect與React.forwardRef結(jié)合ref失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
解決React?hook?'useState'?cannot?be?called?in?
這篇文章主要為大家介紹了React?hook?'useState'?cannot?be?called?in?a?class?component報(bào)錯(cuò)解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
React?正確使用useCallback?useMemo的方式
這篇文章主要介紹了React?正確使用useCallback?useMemo的方式,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
React 性能優(yōu)化之非必要的渲染問(wèn)題解決
本文主要介紹了React 性能優(yōu)化之非必要的渲染問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
React18之update流程從零實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了React18之update流程從零實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

