美文网首页
高阶组件

高阶组件

作者: 海豚先生的博客 | 来源:发表于2020-06-25 18:00 被阅读0次
  • higher-order-component (高阶组件HOC)类似于高阶函数,它接受一个React组件作为参数,返回一个新的React组件。
  • 通俗点讲就是:当React组件被包裹时(wrapped),高阶组件会返回一个加强的React组件。 高阶组件让我们的代码更具有复用性、逻辑性和抽象特征。它可以对render方法作劫持,也可以控制props和state
  • 实现高阶组件有两种方式:
    属性代理(props proxy)。高阶组件通过被包裹的React组件来操作props
    反向继承(inheritance inversion)。 高阶组件继承于被包裹的React组件。
class InBox extends React.Component {
  handleClick () {
    console.log(this)
  }
  render() {
    return (
      <div>
        <div>this.is {this.props.a}+{this.props.b}</div>
        <div onClick={::this.handleClick} >点击</div>
      </div>
    )
  }
}
function HOC(InboxInstance) {
  return class box extends React.Component {
    fn = (InboxInstance) => {
      console.log(InboxInstance)
      // 调用Inbox组件中的方法
      InboxInstance.handleClick()
    }
    render() {
      let props = {
        ...this.props,
        a: 'aaa',
        b: 'bbb'
      }
      props = Object.assign({},this.props,{ref:this.fn.bind(this)})
      return (
        // 控制props
        <InBox {...props} />
      )
    }
  }
}
export default HOC(InBox)
 
//->render 结果: this.is aaa+bbb

稍作修改官方的例子

// 父组件
import Hoc from "./Hoc"
class Fancy extends React.Component {
  constructor(props) {
    super(props)
    this.ref = React.createRef()
  }
  handleClick = () => {
    console.log(this.ref.current) // h2元素
      this.setState({  // 触发更新函数
      labbel: "点我",
    })
  }
  render() {
    return (
      <Hoc label={this.state.label} handleClick={this.handleClick} ref={this.ref} />
    )
  }
}
// 子组件
function logProps(WrappedComponent) {
  class LogProps extends React.Component {
    componentDidUpdate(prevProps) {
      console.log("old props:", prevProps)
      console.log("new props:", this.props)
    }

    render() {
      const { forwardedRef, ...rest } = this.props
      return <WrappedComponent ref={forwardedRef} {...rest} />
    }
  }

  return React.forwardRef((props, ref) => {
    return <LogProps {...props} forwardedRef={ref} />
  })
}

const FancyButton = React.forwardRef((props, ref) => {
  const { handleClick, label } = props
  return (
    <div>
      <h1>{"hello"}</h1>
      <h2 ref={ref} onClick={handleClick}>
        {label}
      </h2>
    </div>
  )
})

export default logProps(FancyButton)

相关文章

  • React-Native 高阶组件

    高阶函数 高阶组件(属性代理)普通组件还可以向高阶组件传值 高阶组件(反向继承) 普通组件的 static 方法怎...

  • React高阶组件HOC

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

  • react与vue中高阶组件的对比

    由高阶函数引申出来的高阶组件 高阶组件本质上也是一个函数,并不是一个组件,且高阶组件是一个纯函数。高阶组件,顾名思...

  • 2021-08-05-🦕🦕 react 高阶组件hotc和@装饰

    简介 高阶组件可以直接调用子组件属性方法;子组件通过 this.props.xxx调用高阶组件方法属性 高阶组件无...

  • React——第三阶段(1)(高阶组件、context)

    根据胡子大哈的文章学习,感谢大胡分享胡子大哈-高阶组件、context 高阶组件 什么是高阶组件 高阶组件就是一个...

  • React 高阶组件(HOC)

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

  • 高阶组件

    高阶组件 先来引入这个概念 高阶函数是什么? 高阶函数就是一个函数返回一个函数eg: 高阶组件 类同 高阶组件就是...

  • React 进阶之高阶组件

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

  • 利用 React 高阶组件实现一个面包屑导航

    什么是 React 高阶组件 React 高阶组件就是以高阶函数的方式包裹需要修饰的 React 组件,并返回处理...

  • Vue组件

    1,高阶组件:组件

网友评论

      本文标题:高阶组件

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