react 高阶组件

高阶组件本质是函数,参数是 组件1 返回组件2,高阶组件是为了复用通用逻辑

高阶组件eg:

import React, { Component } from 'react';

const withPersistentData = data => WrappedComponent => {

  return class extends Component {
    constructor(props) {
      super(props);
    }
    render() {
      return (
        <WrappedComponent data={data} />
      )
    }
  }
}
export default withPersistentData

尝试用定义好的高阶组件

import React, { Component } from 'react';
import HighComponent from './HighComponent';

class LowComponent extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div>{this.props.data}</div>
    )
  }
}

const MyComponent = HighComponent('Hello')(LowComponent)
export default MyComponent

    原文作者:ilvseyinfu
    原文地址: https://segmentfault.com/a/1190000018328170
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞