React HOC(高阶组件)
先看这篇理解高阶组件 https://github.com/brickspert/blog/issues/2
一、定义
高阶函数:函数接受函数作为输入,或者输出一个函数。
高阶组件:接受React组件作为输入,或是输出一个组件。即hocFactory:: W: React.Component => E: React.Component
高阶组件让代码更有复用性、逻辑性、抽象性。
二、实现高阶函数的三种方法。
1. 属性继承(将Input组件的name,onChange方法提取到高阶组件中)
const Mycontainer = (WrappedComponent) => {
class extends React.Component {
constructor(props) {
super(props);
this.state = {
name: '',
}
this.onNameChange = this.onNameChange.bind(this);
}
onNameChange(event) {
this.state({
name: event.target.value,
})
}
render() {
const newProps = { name:{
value: this.state.name,
onChange: this.onNameChange,
} };
return <WrappedComponent {...this.props} {...newProps}/>;
}
}
}
class MyComponent extends React.Component {
// ...
}
export default Mycontainer(MyComponent);
2. 反向继承
渲染劫持: 就是高阶组件可以控制组件渲染以及props,state等。
const MyContainer = WrappedComponent =>
class extends WrappedComponent {
render() {
if (this.props.loggedIn) {
return super.render();
} else {
return null;
}
}
}
3. 组件参数
调用高阶组件时需要传递一些参数
function HOCFactoryFactory(...params) {
// 可以做一些改变params的事情
return function HOCFactory(WrappedComponent) {
return class HOC extends Component {
render() {
return <WrappedComponent {...this.props} {...params}/>
}
}
}
}
HOCFactoryFactory(params)(WrappedComponent);
三、使用高阶组件使组件分离,抽象逻辑。
class SelectInput extends React.Component {
static displayName = 'SelectInput';
render() {
const { selectedItem, isActive, onClickHeader, placeholder } = this.props;
const { text } = selectedItem;
return (
<div>
<div onClick={onClickHeader}>
<input type="text" disabled value={text} placeholder={placeholder}/>
<Icon className={isActive} name="angle-name"/>
</div>
</div>
);
}
}
const searchDecorator = WrappedComponent => {
class SearchDecorator extends React.Component {
constructor(props){
super(props);
this.handleSearch = this.handleSearch.bind(this);
}
handleSearch(keyword) {
this.setState({
data: this.props.data,
keyword,
});
this.props.onSearch(keyword);
}
render() {
const { data, keyword } = this.state;
return (
<WrappedComponent
{...this.props}
data={data}
keyword={keyword}
onSearch={this.handleSearch}
/>
)
}
}
return SearchDecorator;
}
const asyncSelectDecorator = WrappedComponent => {
class AsyncSelectDecorator extends React.Component {
componentDidMount() {
const { url, params } = this.props;
fetch(url, { params }).then(data => {
this.setState({
data,
});
});
}
render() {
return (
<WrappedComponent
{...this.props}
data={this.state.data}
/>
)
}
}
return AsyncSelectDecorator;
}
class App extends React.Component {
render() {
const Input = searchDecorator(asyncSelectDecorator(SelectInput))
return {
<Input {...this.props} />
}
}
}
React HOC(高阶组件)的更多相关文章
- 【转】react的高阶组件
React进阶之高阶组件 前言 本文代码浅显易懂,思想深入实用.此属于react进阶用法,如果你还不了解react,建议从文档开始看起. 我们都知道高阶函数是什么, 高阶组件其实是差不多的用法,只 ...
- React 之 高阶组件的理解
1.基本概念 高阶组件是参数为组件,返回值为新组件的函数. 2.举例说明 ① 装饰工厂模式 组件是 react 中的基本单元,组件中通常有一些逻辑(非渲染)需要复用处理.这里我们可以用高阶组件对组件内 ...
- react.js 高阶组件----很简单的实例理解高阶组件思想
调试代码之前,我设置了两个缓存 分别是username和content 在控制台console设置两个缓存代码 localStorage.setItem('username','老王')localSt ...
- react之高阶组件(二)
高阶组件的使用 接上文———— 一.像函数一样直接调用 import React, { Component } from 'react' import A from './A' class C ext ...
- react用高阶组件实现路由守卫
react-router不像vue-router一样有很多钩子函数,可以做路由守卫.想实现路由守卫,可以用高阶组件来实现. @connect(state => ({ isLogin: state ...
- react之高阶组件(一)
当两个或多个组件有相同的地方,可以将相同的部分抽离出来 先创建三个组件A.B.C A.js import React, { Component } from 'react' class A exten ...
- React高阶组件总结
在多个不同的组件中需要用到相同的功能,这个解决方法,通常有Mixin和高阶组件. Mixin方法例如: //给所有组件添加一个name属性 var defaultMixin = { getDefaul ...
- React高阶组件 和 Render Props
高阶组件 本质 本质是函数,将组件作为接收参数,返回一个新的组件.HOC本身不是React API,是一种基于React组合的特而形成的设计模式. 解决的问题(作用) 一句话概括:功能的复用,减少代码 ...
- React中的高阶组件,无状态组件,PureComponent
1. 高阶组件 React中的高阶组件是一个函数,不是一个组件. 函数的入参有一个React组件和一些参数,返回值是一个包装后的React组件.相当于将输入的React组件进行了一些增强.React的 ...
- React-理解高阶组件
高阶组件:定义一个函数,传入一个组件,返回另外一个组件,另外一个组件包裹了传入的组件. 分类:属性代理高阶组件,反向继承高阶组件. 作用:代码复用,渲染节时. 高阶函数例子: function hel ...
随机推荐
- 数据结构与算法 --- js描述栈
js描述栈及栈的使用 栈的特性就是只能通过一端访问,这一段就是叫做栈顶.咖啡馆内的一摞盘子就是最形象的栈的例子: 根据栈的特性,就可以定义栈的一些特殊属性和方法;用js的描述栈的时候底层数据结构用的是 ...
- 吉哥系列故事——恨7不成妻(数位dp)
吉哥系列故事--恨7不成妻 传送门 Problem Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥 ...
- PMP——项目管理的价值观与方法论
关于项目管理的十个成语: 未雨绸缪(计划.风险):识别风险.做出计划.并指定负责人: 防微杜渐(监控.纠正):持续的实时的监控计划,监控和发现偏差,并进行纠正: 资源集成(整合.采购):把最专业的资源 ...
- mybatis入门案例自定义实现
mybatis入门案例自定义实现 一.需要实现的类和接口 public static void main(String[] args) throws Exception{ //1.读取配置文件 Inp ...
- ajax 后台java代码执行完毕 前端报404错误
一个ajax请求,到java后台代码,后台成功接受并执行相应处理,但是返回的时候,success却没进去,前端报404错误. 因为是由于Controller忘记写spring的@Responsebod ...
- postgresql spi开发笔记
#include "postgres.h" #include "fmgr.h" #include <string.h> #ifdef PG_MODU ...
- LUA提取免费迅雷账号
--获取http://www.521xunlei.com/ 免费迅雷账号 function getPageid() local http = require("socket.http&quo ...
- 使用stringstream打破字符与其他类型之间的隔阂
考虑这样一个问题:给您一行各位数字,计算它们的和.数字之间用空格隔开.只能使用字符串. 一般考虑使用getchar,但这对于不熟悉 ASCLL 码的同学十分困难.C++的sstream头文件中提供了十 ...
- python学习(1)python的基本概念
1.python是世界上最流行的程序语言之一,用途广泛. 2.python是解释型语言,与C++编译类语言相比,python扩展性强,简单易上手.但是缺点也很明显,执行速度慢. 3.python定义中 ...
- step1:准备歌词之《前端开发是个啥》
以下是给大家介绍前端开发的填词,曲子是李圣杰的<最近>,大家喜欢可以试试唱. 点赞关注超过100的平台,我后续上来发本人原唱视频(目前正在练习中...),另外大家觉得哪些词写得不好的,欢迎 ...