美文网首页
# 高阶组件 HOC

# 高阶组件 HOC

作者: 迷缘火叶 | 来源:发表于2019-06-25 11:46 被阅读0次

高阶组件就是一个函数,且该函数接受一个组件作为参数,并返回一个新的组件。

高阶函数模拟

高阶函数就是一个接收一个函数并返回另外一个新的函数!

function welcome(username) {
    console.log('welcome ' + username);
}

function goodbey(username) {
    console.log('goodbey ' + username);
}

function wrapWithUsername(wrappedFunc) {
    let newFunc = () => {
        let username = localStorage.getItem('username');
        wrappedFunc(username);
    };
    return newFunc;
}

welcome = wrapWithUsername(welcome);
goodbey = wrapWithUsername(goodbey);

welcome();
goodbey();

wrapWithUsername函数就是一个“高阶函数”, 处理了username,传递给目标函数

高阶组件——代理

没有副作用的纯函数

  • 操作props
  • 访问ref(不推荐)
  • 抽取状态
  • 包装组件

props

删除props:

import React from 'react';

function HOCRemoveProps (WrappedComponent) {
  return class WrappingComPonent extends React.Component {
    render () {
      const {user, ...otherProps} = this.props;
      // 去除 user
      return <WrappedComponent {...otherProps} />
    }
  }
}
export default HOCRemoveProps;

增加props:

import React from 'react';

function HOCAddProps (WrappedComponent, uid) {
  return class WrappingComPonent extends React.Component {
    render () {
      const newProps = {
        uid
      };
      return <WrappedComponent {...this.props} {...newProps} />
    }
  }
}
export default HOCAddProps;

抽取状态

将所有的状态的管理交给外面的容器组件,这个模式就是 抽取状态

import React from 'react';

const HOCContainer = (WrappedComponent) => {
  class extends React.Component {
    constructor (props) {
      super(props);
      this.state = {
        name: ''
      }
    }

    onNameChange = (event) => {
      this.setState({
        name: event.target.value
      });
    }

    render () {
      const newProps = {
        value: this.state.name,
        onChange: this.onNameChange
      };

      return <WrappedComponent {...this.props} {...newProps} />
    }
  }
}

export default HOCContainer;


@HOCContainer

class SampleComponent extends React.Component {
  render () {
    return <input name="name" {this.props.name} />
  }
}

包装组件

import React from 'react';

function HOCStyleComponent (WrappedComponent, style) {
  return class WrappingComPonent extends React.Component {
    render () {
      return (
        <div style={style}>
          <WrappedComponent {...this.props} />
        </div>
      )
    }
  }
}
export default HOCStyleComponent;

高阶组件——继承

  • 操作生命周期
  • 操作props

操作生命周期

控制render

import React from 'react';

const HocComponent = (WrappedComponent) => {
  class MyContainer extends WrappedComponent {
    render () {
      const eleTree = super.render();
      const newProps = Object.assign({}, eleTree.props);
      if (this.props.time && this.state.success) {
        return eleTree;
      }

      return <div>倒计时完成了...</div>
    }
  }
}

export default HocComponet;

示例

有时使用高阶组件,不能获取到真正包装的组件ref, 这时可以采用高阶组件解决这个问题

/**
 * @name withRef
 * @func  getInstance
 * @description 处理父组件从子组件(包含HOC包裹的)获取ref实例
 * @example
 
 @withRef  // 注意:这句必须写在最接近`childComponent`的地方
 */

import React from 'react';

export default (WrappedComponent) => {
  return class withRef extends React.Component {
    static displayName = `withRef(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
    render () {
      // 这里重新定义一个props的原因是:
      // 你直接去修改this.props.ref在react开发模式下会报错,不允许你去修改
      const props = {
        ...this.props,
      };
      // 在这里把getInstance赋值给ref,
      // 传给`WrappedComponent`,这样就getInstance能获取到`WrappedComponent`实例
      props.ref = (el)=>{
        this.props.getInstance && this.props.getInstance(el);
        this.props.ref && this.props.ref(el);
      };
      return (
        <WrappedComponent {...props} />
      );
    }
  };
};

相关文章

  • React 进阶之高阶组件

    高阶组件 HOC 高阶组件(HOC)是react中的高级技术,用来重用组件逻辑。但高阶组件本身并不是React A...

  • ReactNative中的高阶组件(HOC)和继承详解

    ReactNative中的高阶组件(HOC)和继承详解 共同点: 高阶组件(HOC)是 React 中用于复用组件...

  • 高阶组件

    Hoc(高阶组件) 概念 hoc基本用法 hoc链式调用 hoc装饰器用法 概念 概念: 接受组件, 返回新组件,...

  • React 高阶组件(HOC)

    什么是高阶组件? 高阶组件(Higher-Order Components,简称HOC):简而言之,高阶组件就是加...

  • React高阶组件(HOC)

    高阶组件(Higher-Order Components) 高阶组件(HOC)是 React 中用于重用组件逻辑的...

  • 高阶组件

    React 高阶组件HOC (Higher-Order Component) 高阶组件是react中重复使用组件逻...

  • React进阶篇(一)高阶组件

    高阶组件 高阶组件(Higher Order Component,HOC)是React的一种设计模式,用于增强现有...

  • 从高阶函数到高阶组件

    介绍 高阶组件(HOC)是 React 中用于复用组件逻辑的一种高级技巧。HOC 自身不是 React API 的...

  • React 学习笔记二 - HOC 高阶组件理解

    官方定义 高阶组件(HOC)是 React 中用于 复用组件逻辑 的一种高级技巧。HOC 自身不是 React A...

  • 高阶组件

    higher-order-component (高阶组件HOC)类似于高阶函数,它接受一个React组件作为参数,...

网友评论

      本文标题:# 高阶组件 HOC

      本文链接:https://www.haomeiwen.com/subject/qaltcctx.html