【React】HOC高阶组件

定义

高阶组件:High Oder Component,入参为组件,出参也为组件。且不会改变参数组件,所以该函数是个纯函数,不会有副作用。

高阶组件可以对参数组件做统一处理逻辑,常见场景如:权限控制属性代理

作用

  • 功能代码的抽象,提升复用性
  • 控制组件的渲染流程
  • 检测组件性能

属性代理

方式一:props

函数组件

function HOC(WrappedComponent) {
	const newProps = { type:'HOC' }
	return props => <WrappedComponent {...props} {...newProps}/>
}

类组件

function HOC(WrappedComponent) {
	return class extend React.Component {
		render() {
			const newProps = { type: 'HOC' }
			return <WrappedComponent {...this.props} {...newProps}/>
		}
	}
}

方式二:抽象state

模拟给参数组件中添加state

function HOC(WrappedComponent){
  return class extends React.Component{
    constructor(props){
      super(props)
      this.state = {name:'田本初'}
      this.onChange = this.onChange.bind(this)
    }
    onChange(e){
      this.setState({name:e.target.value})
    }
    render() {
      const newProps = {
        name: this.state.name,
        onChange: this.onChange
      }
        return <WrappedComponent {...this.props}{...newProps}/>
    }
  }
}

方式三: 控制渲染逻辑

function HOC(WrappedComponent){
  if(props.type === '1'){
    return <div className="style1"><WrappedComponent/></div>
  }else if(props.type === '2'){
    return <div className="style2"><WrappedComponent/></div>
  }else{
    return <div>xxx</div>
  }
}

反向继承

通过直接继承参数组件,创建一个新组件并返回。

function HOC(WrappedComponent){
  const didMount = WrappedComponent.prototype.componentDidMount
  return class extend WrappedComponent{
  	// 对参数组件的生命周期进行修改
    async componentDidMount(){
      if(didMount){
        await didMount.apply(this)
        // ....其他操作
      }
    }
    render(){
      return super.render()
    }
  }
}

这种方式更加灵活,可以任意进行渲染逻辑的控制以及修改state。

function HOC(WrappedComponent){
  return class extend WrappedComponent{
    render(){
     const tree = super.render()
     const newProps = {}
     if(tree && tree.type === 'input'){
      newProps.value = '田本初'
     }

     const props = { ...tree.props,...newProps }
     const newTree = React.cloneElement(tree,props,tree.props.children)
     
     return newTree
    }
  }
}

计算组件渲染耗时

function withTime(WrappedComponent){
  return class extend WrappedComponent{
    constructor(props){
      super(props)
      start,
      end
    }

    componentWillMount(){
      if(super.somponentWillMount){
        super.componentWillMount()
      }
      start = +new Date()
    }

    componentDidMount(){
      if(super.componentDidMount){
        super.componentDidMount()
      }
      end = +new Date()
      console.log(`渲染耗时${end - start}ms`)
    }

    render(){
      return super.render()
    }
  }
}

属性代理与反向继承对比

二者同样对参数组件进行处理,但是反向继承属性代理更加强大,不同于属性代理只能操作组件外部,反向继承相当于复制了一个参数组件,并进行修改,更加灵活。

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田本初

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值