網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
目錄
一 使用步驟
二 顯示鼠標(biāo)坐標(biāo)
三 鼠標(biāo)隨圖片移動(dòng)
四 設(shè)置displayName
五 傳遞props
高階組件HOC: Higher-Order Component,是一個(gè)函數(shù),接收要包裝的組件,返回增強(qiáng)后的組件
目的:實(shí)現(xiàn)狀態(tài)邏輯復(fù)用
高階組件就相當(dāng)于手機(jī)殼, 采用包裝(裝飾)模式
一 使用步驟
1 創(chuàng)建一個(gè)函數(shù),名稱約定以with開(kāi)頭
2 指定函數(shù)的參數(shù),參數(shù)以大寫(xiě)字母開(kāi)頭(作為要渲染的組件)
3 在函數(shù)內(nèi)部創(chuàng)建一個(gè)類組件,提供復(fù)用的狀態(tài)邏輯代碼,并返回
4 渲染參數(shù)組件,同時(shí)將狀態(tài)通過(guò)props傳遞給參數(shù)組件
5 調(diào)用該高階組件,傳入要增強(qiáng)的組件,通過(guò)返回值拿到增強(qiáng)后的組件,并將其渲染到頁(yè)面中
二 顯示鼠標(biāo)坐標(biāo)
import React from "react";
import ReactDOM from "react-dom";
// 1 創(chuàng)建一個(gè)函數(shù),名稱約定以with開(kāi)頭
// 2 指定函數(shù)的參數(shù),參數(shù)以大寫(xiě)字母開(kāi)頭(作為要渲染的組件)
function withMouse(WrappedComponent) {
// 3 在函數(shù)內(nèi)部創(chuàng)建一個(gè)類組件,提供復(fù)用的狀態(tài)邏輯代碼,并返回
class Mouse extends React.Component {
//鼠標(biāo)狀態(tài)
state = {
x: 0,
y: 0
}
//處理狀態(tài)的邏輯代碼
handleMouseMove = e => {
this.setState({
x: e.clientX,
y: e.clientY
})
}
//監(jiān)聽(tīng)鼠標(biāo)移動(dòng)事件
componentDidMount() {
window.addEventListener("mousemove", this.handleMouseMove)
}
//解綁事件
componentWillUnmount() {
window.removeEventListener('mousemove', this.handleMouseMove)
}
render() {
//4 渲染參數(shù)組件,同時(shí)將狀態(tài)通過(guò)props傳遞給參數(shù)組件
return <WrappedComponent {...this.state}/>
}
}
return Mouse;
}
const Position = props => (
<p>
鼠標(biāo)當(dāng)前位置:(x:{props.x},y:{props.y})
</p>
)
//調(diào)用該高階組件,傳入要增強(qiáng)的組件,通過(guò)返回值拿到增強(qiáng)后的組件,并將其渲染到頁(yè)面中
const MousePosition = withMouse(Position)
class App extends React.Component {
render() {
return (
<div>
{/*渲染增強(qiáng)后的組件*/}
<MousePosition/>
</div>)
}
}
ReactDOM.render(<App/>, document.getElementById("root"));
效果
三 鼠標(biāo)隨圖片移動(dòng)
加入鼠標(biāo)隨圖片移動(dòng)的功能
import imgage from "./images/cat.jpg"
const Cat = props => (
<img src={imgage} alt='貓' style={{
position: 'absolute',
// 為了讓鼠標(biāo)在圖片的中間,top減掉了圖片的一半高度,left減掉了圖片一半的寬度
top: props.y - 180,
left: props.x - 180
}}/>
)
//調(diào)用該高階組件,傳入要增強(qiáng)的組件,通過(guò)返回值拿到增強(qiáng)后的組件,并將其渲染到頁(yè)面中
const MousePosition1 = withMouse(Cat)
完整代碼
import React from "react";
import ReactDOM from "react-dom";
import imgage from "./images/cat.jpg"
// 1 創(chuàng)建一個(gè)函數(shù),名稱約定以with開(kāi)頭
// 2 指定函數(shù)的參數(shù),參數(shù)以大寫(xiě)字母開(kāi)頭(作為要渲染的組件)
function withMouse(WrappedComponent) {
// 3 在函數(shù)內(nèi)部創(chuàng)建一個(gè)類組件,提供復(fù)用的狀態(tài)邏輯代碼,并返回
class Mouse extends React.Component {
//鼠標(biāo)狀態(tài)
state = {
x: 0,
y: 0
}
//處理狀態(tài)的邏輯代碼
handleMouseMove = e => {
this.setState({
x: e.clientX,
y: e.clientY
})
}
//監(jiān)聽(tīng)鼠標(biāo)移動(dòng)事件
componentDidMount() {
window.addEventListener("mousemove", this.handleMouseMove)
}
//解綁事件
componentWillUnmount() {
window.removeEventListener('mousemove', this.handleMouseMove)
}
render() {
//4 渲染參數(shù)組件,同時(shí)將狀態(tài)通過(guò)props傳遞給參數(shù)組件
return <WrappedComponent {...this.state}/>
}
}
return Mouse;
}
const Cat = props => (
<img src={imgage} alt='貓' style={{
position: 'absolute',
// 為了讓鼠標(biāo)在圖片的中間,top減掉了圖片的一半高度,left減掉了圖片一半的寬度
top: props.y - 125,
left: props.x - 100
}}/>
)
//調(diào)用該高階組件,傳入要增強(qiáng)的組件,通過(guò)返回值拿到增強(qiáng)后的組件,并將其渲染到頁(yè)面中
const MousePosition1 = withMouse(Cat)
const Position = props => (
<p>
鼠標(biāo)當(dāng)前位置:(x:{props.x},y:{props.y})
</p>
)
//調(diào)用該高階組件,傳入要增強(qiáng)的組件,通過(guò)返回值拿到增強(qiáng)后的組件,并將其渲染到頁(yè)面中
const MousePosition2 = withMouse(Position)
class App extends React.Component {
render() {
return (
<div>
{/*渲染增強(qiáng)后的組件*/}
<MousePosition1/>
<MousePosition2/>
</div>)
}
}
ReactDOM.render(<App/>, document.getElementById("root"));
四 設(shè)置displayName
默認(rèn)情況下,react使用組件名稱作為displayName
使用高階組件存在的問(wèn)題: 得到的兩個(gè)組件名稱相同
解決辦法: 設(shè)置displayName → 用于調(diào)試時(shí)區(qū)分不同的組件
效果
完整代碼
import React from "react";
import ReactDOM from "react-dom";
import imgage from "./images/cat.jpg"
// 1 創(chuàng)建一個(gè)函數(shù),名稱約定以with開(kāi)頭
// 2 指定函數(shù)的參數(shù),參數(shù)以大寫(xiě)字母開(kāi)頭(作為要渲染的組件)
function withMouse(WrappedComponent) {
// 3 在函數(shù)內(nèi)部創(chuàng)建一個(gè)類組件,提供復(fù)用的狀態(tài)邏輯代碼,并返回
class Mouse extends React.Component {
//鼠標(biāo)狀態(tài)
state = {
x: 0,
y: 0
}
//處理狀態(tài)的邏輯代碼
handleMouseMove = e => {
this.setState({
x: e.clientX,
y: e.clientY
})
}
//監(jiān)聽(tīng)鼠標(biāo)移動(dòng)事件
componentDidMount() {
window.addEventListener("mousemove", this.handleMouseMove)
}
//解綁事件
componentWillUnmount() {
window.removeEventListener('mousemove', this.handleMouseMove)
}
render() {
//4 渲染參數(shù)組件,同時(shí)將狀態(tài)通過(guò)props傳遞給參數(shù)組件
return <WrappedComponent {...this.state}/>
}
}
//設(shè)置displayName
Mouse.displayName = `withMouse${getDisplayName(WrappedComponent)}`
return Mouse;
}
//displayName
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
}
const Cat = props => (
<img src={imgage} alt='貓' style={{
position: 'absolute',
// 為了讓鼠標(biāo)在圖片的中間,top減掉了圖片的一半高度,left減掉了圖片一半的寬度
top: props.y - 125,
left: props.x - 100
}}/>
)
//調(diào)用該高階組件,傳入要增強(qiáng)的組件,通過(guò)返回值拿到增強(qiáng)后的組件,并將其渲染到頁(yè)面中
const MousePosition1 = withMouse(Cat)
const Position = props => (
<p>
鼠標(biāo)當(dāng)前位置:(x:{props.x},y:{props.y})
</p>
)
//調(diào)用該高階組件,傳入要增強(qiáng)的組件,通過(guò)返回值拿到增強(qiáng)后的組件,并將其渲染到頁(yè)面中
const MousePosition2 = withMouse(Position)
class App extends React.Component {
render() {
return (
<div>
{/*渲染增強(qiáng)后的組件*/}
<MousePosition1/>
<MousePosition2/>
</div>)
}
}
ReactDOM.render(<App/>, document.getElementById("root"));
五 傳遞props
問(wèn)題:
添加了一個(gè)屬性, 打印了props
屬性沒(méi)顯示在props里
原因:props傳遞丟失
解決辦法:
效果
完整代碼
import React from "react";
import ReactDOM from "react-dom";
// 1 創(chuàng)建一個(gè)函數(shù),名稱約定以with開(kāi)頭
// 2 指定函數(shù)的參數(shù),參數(shù)以大寫(xiě)字母開(kāi)頭(作為要渲染的組件)
function withMouse(WrappedComponent) {
// 3 在函數(shù)內(nèi)部創(chuàng)建一個(gè)類組件,提供復(fù)用的狀態(tài)邏輯代碼,并返回
class Mouse extends React.Component {
//鼠標(biāo)狀態(tài)
state = {
x: 0,
y: 0
}
//處理狀態(tài)的邏輯代碼
handleMouseMove = e => {
this.setState({
x: e.clientX,
y: e.clientY
})
}
//監(jiān)聽(tīng)鼠標(biāo)移動(dòng)事件
componentDidMount() {
window.addEventListener("mousemove", this.handleMouseMove)
}
//解綁事件
componentWillUnmount() {
window.removeEventListener('mousemove', this.handleMouseMove)
}
render() {
//4 渲染參數(shù)組件,同時(shí)將狀態(tài)通過(guò)props傳遞給參數(shù)組件
return <WrappedComponent {...this.state}{...this.props}/>
}
}
return Mouse;
}
const Position = props => {
console.log(props)
return (
<p>
鼠標(biāo)當(dāng)前位置:(x:{props.x},y:{props.y})
</p>
)
}
//調(diào)用該高階組件,傳入要增強(qiáng)的組件,通過(guò)返回值拿到增強(qiáng)后的組件,并將其渲染到頁(yè)面中
const MousePosition = withMouse(Position)
class App extends React.Component {
render() {
return (
<div>
{/*渲染增強(qiáng)后的組件*/}
<MousePosition a="1"/>
</div>)
}
}
ReactDOM.render(<App/>, document.getElementById("root"));
原文鏈接:https://blog.csdn.net/m0_45877477/article/details/125896275
相關(guān)推薦
- 2022-04-25 Pycharm報(bào)錯(cuò):'NoneType'?object?has?no?attribute?'byte
- 2022-05-18 python操作jira添加模塊的方法_python
- 2022-06-22 C語(yǔ)言詳解判斷相同樹(shù)案例分析_C 語(yǔ)言
- 2022-09-24 Python?Matplotlib通過(guò)plt.subplots創(chuàng)建子繪圖_python
- 2022-05-04 Spring.Net框架簡(jiǎn)介_(kāi)實(shí)用技巧
- 2023-06-04 Pandas通過(guò)index選擇并獲取行和列_python
- 2022-07-09 C++深入探索內(nèi)聯(lián)函數(shù)inline與auto關(guān)鍵字的使用_C 語(yǔ)言
- 2022-10-04 python中numpy矩陣的零填充的示例代碼_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支