步入React正殿 - React组件设计模式

news/2025/2/19 8:27:06/

目录

扩展学习资料

高阶组件

@/src/components/hoc/withTooltip.js

@/src/components/hoc/itemA.jsx

@/src/components/hoc/itemB.jsx

@/src/App.js

函数作为子组件【Render pprops】

函数作为子组件

@/src/components/rp/itemC.jsx【父组件】

@/src/components/rp/withTooltip.js【子组件】

练习


扩展学习资料

资料名称

链接

扩展阅读

React组件Render Props VS HOC 设计模式 - 简书

扩展阅读

React Hooks 之于 HoC 与 Render Props - 知乎

高阶组件

复用业务逻辑:判断用户是否是vip:是->有列表,有推荐

一个组件—高阶函数—>新的逻辑的组件

高阶组件是对已有组件的封装形成新的组件之后有自己的状态和逻辑并可以传递已有的组件

const NewComponent = higherOrderComponent(OldComponent)

hoc【higherOrderComponent】

@/src/components/hoc/withTooltip.js

import React from 'react';
// 带工具提示【函数组件】
const withTooltip = (Component) => {class HOC extends React.Component {state = {showToolTip: false,content: '',};handleOver = (event) => {this.setState({showToolTip: true,content: event.target.innerText,});};handleOut = () => {this.setState({showToolTip: false,content: '',});};render() {return (<div onMouseOver={this.handleOver} onMouseOut={this.handleOut}><Component action={this.state} {...this.props} /></div>);}}return HOC;
};
export default withTooltip;

@/src/components/hoc/itemA.jsx

import React from 'react';
import withTooltip from './withTooltip';
// 一个简单的带工具提示-业务组件A
const ItemA = (props) => {return (<div className='container'><button className='btn btn-primary' type='btn'>Tooltip A</button>{props.action.showToolTip && (<span className='badge badge-pill badge-primary ml-2'>{props.action.content}</span>)}</div>);
};
export default withTooltip(ItemA);

@/src/components/hoc/itemB.jsx

import React from 'react';
import withTooltip from './withTooltip';
// 一个简单的带工具提示-业务组件B
const ItemB = (props) => {return (<div className='container'><button className='btn btn-danger' type='btn'>Tooltip B <i>斜体</i>、<b>粗体</b></button>{props.action.showToolTip && (<span className='badge badge-pill badge-danger ml-2'>{props.action.content}</span>)}</div>);
};
export default withTooltip(ItemB);

@/src/App.js

import React, { PureComponent } from 'react';
import ItemA from './components/hoc/itemA';
import ItemB from './components/hoc/itemB';
class App extends PureComponent {render() {console.log('App - rendering');return (<><ItemA id="1" /><ItemB id="2" /></>);}
}
export default App;

ItemA,ItemB都需要相同的withTooltip【props.action】显示逻辑,只需要将withTooltip封装就能得到一个将已有组件封装为高阶组件高阶(封装)函数

  • 一个函数,传入一个组件,返回一个新组件
  • 一般不会有UI展现
  • 提供一些可复用的功能

函数作为子组件【Render pprops】

解决复用逻辑的问题

函数作为子组件

 1.定义子组件

// 子组件
render () {return (<div>{this.props.render(this.state)}</div>                )
}

2.使用函数作为Props

// 父组件
<RenderPropComponent render={(state)=>(<div>content</div>
)}>

@/src/components/rp/itemC.jsx【父组件】

import React from 'react';
import WithTooltip from './withTooltip';
// 一个简单的带工具提示-业务组件A
const ItemC = (props) => {return (<div className='container'><WithTooltiprender={({ showToolTip, content }) => (<div><button className='btn btn-primary' type='btn'>Tooltip C</button>{showToolTip && (<span className='badge badge-pill badge-primary ml-2'>{content}</span>)}</div>)}>{({ showToolTip, content }) => (<div><button className='btn btn-primary' type='btn'>Tooltip D</button>{showToolTip && (<span className='badge badge-pill badge-primary ml-2'>{content}</span>)}</div>)}</WithTooltip></div>);
};
export default ItemC;

@/src/components/rp/withTooltip.js【子组件】

import React from 'react';
class WithTooltip extends React.Component {// // eslint-disable-next-line no-useless-constructor// constructor(props) {//  super(props);// }state = {showToolTip: false,content: '',};handleOver = (event) => {this.setState({showToolTip: true,content: event.target.innerText,});};handleOut = () => {this.setState({showToolTip: false,content: '',});};render() {return (<div onMouseOver={this.handleOver} onMouseOut={this.handleOut}>{this.props.render && this.props.render(this.state)}{this.props.children && this.props.children(this.state)}</div>);}
}
export default WithTooltip;

练习

【题目1】分别使用Render Props和HOC模式实现购物车ToolTips功能;

【题目2】说明Render Props 和 HOC设计模式的优缺点分别是什么;


http://www.ppmy.cn/news/1039467.html

相关文章

责任链模式简单实现

两种实现方式 第一种 public interface IBaseTask {public void doAction(String isTask,IBaseTask iBaseTask); }public class ChainManager implements IBaseTask{//工作类的集合private List<IBaseTask> iBaseTaskList new ArrayList<>();public void addTas…

【分享】使用FileChannel进行文件拷贝

前言: 项目实际编写中&#xff0c;使用到了多种文件拷贝方式&#xff0c;有包括专门使用c写了拷贝工具&#xff0c;供给Java调用&#xff0c;也有使用标准的输入输出流&#xff0c;这里分享的是借助 FileChannel 来读写,nio中传送数据使用channelbuffer&#xff0c;大的数据可以…

【RocketMQ】安装

文章目录 下载RocketMQ配置环境变量 下载RocketMQ 下载RocketMQ安装包 下载DashBoard 这里版本推荐选择4.9.x&#xff0c;因为比较稳定。 下载完毕之后&#xff0c;将安装包拖入到Linux环境。 之后使用unzip命令解压缩RocketMQ的安装包。 unzip ./rocketmq-all-4.9.2-bin-rel…

linux 命令--常用关机命令

1.使用shutdown命令 shutdown命令是Linux系统下最常用的关机命令之一。它可以让系统在指定时间内进行关机或者重启操作。例如&#xff0c;下面的命令可以让系统在5分钟后进行关机操作&#xff1a; sudo shutdown -h5其中&#xff0c;“-h”表示关机&#xff0c;“5”表示5分钟…

openstack的role到底怎么用

openstack权限 role&#xff1a;角色&#xff0c;在Openstack的keystone数据库中定义&#xff0c;表示一个角色。 rule: 规则&#xff0c;在Policy.yaml中定义&#xff0c;表示一个或多个role的集合。比如在nova中&#xff0c;rule用两种用法 定义role为rule,如nova中最常用…

09- DMA(DirectMemoryAccess直接存储器访问)

DMA 09 、DMA(DirectMemoryAccess直接存储器访问)DMA配置流程 09 、DMA(DirectMemoryAccess直接存储器访问) DMA配置流程 dma.c文件 main.c文件 详见《stm32中文参考手册》表57。

详解Arrays类的底层源码

1.给数据排序【Arrays.sort()】 //对整型数组进行排序 public static void sort(int[] a) {//底层使用的是快速排序【时间复杂度&#xff1a;O(nlongN)】DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); }//对给定范围的期间内进行排序 ---> [fromIndex&#xf…

651页23万字智慧教育大数据信息化顶层设计及建设方案WORD

导读&#xff1a;原文《651页23万字智慧教育大数据信息化顶层设计及建设方案WORD》&#xff08;获取来源见文尾&#xff09;&#xff0c;本文精选其中精华及架构部分&#xff0c;逻辑清晰、内容完整&#xff0c;为快速形成售前方案提供参考。 目录 一、 方案背景 1.1 以教育…