react高阶组件

高阶组件:

  以接收React组件为参数,并且返回一个新的React组件。

function widthPer(WrapComponent) {
    return class extends Component {

        componentWillMount() {
            this.data = 'i am commom data'
        }

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

 

class AboutState extends Component {

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

 

一、使用场景:

  1.1 操纵props

    通过拦截到props,对props进行包装。

  1.2 通过ref访问组件实例

    通过ref获取包装组件实例的引用。

    

    render() {
            return (
                <WrapComponent ref={ref=>this.instance =ref}/>
            )
        }

  1.3组件状态提升

    把组件状态提升到高阶组件

  1.4用其他元素包装组件

    render() {
            return (
                <div>
                    <WrapComponent ref={ref=>this.instance =ref}/>
                </div>
            )
        }

 

二、参数传递

const widthPer = (key) => (WrapComponent) => {
    return class extends Component {
        componentWillMount() {
            this.data = key
        }

        render() {
            return (
                <div>
                    <WrapComponent ref={ref => this.instance = ref} data={this.data} />
                </div>
            )
        }
    }
}
widthPer('i am param')(AboutState)

 

三、继承方式实现

 
  
function widthPer(WrapComponent) {
    return class extends WrapComponent {
        render() {
            if (this.state.logo) {
                return super.render()
            } else {
                return null
            }
        }
    }
}
 
  
class AboutState extends Component {
    state = {
        logo: true
    }

    render() {
        return (
            <div>
                Hight
            </div>
        )
    }
}

 

 

四、注意事项

  4.1对高阶组件的显示名称自定义做处理

function widthPer(WrapComponent) {
    return class extends WrapComponent {
        static displayName=`HOC(${getDisplayName(WrapComponent)})`
        
        render() {
            if (this.props.logo) {
                return super.render()
            } else {
                return null
            }
        }
    }
}

function getDisplayName(wrapComonent){
    return wrapComonent.displayName||wrapComonent.name||'Component'
}

  4.2定义位置

  不要在组件的render中使用高阶组件,也不要在组件的其他生命周期中使用高阶组件。【每次调用高阶组件都会返回一个新的数组,于是每次render,前一次高阶组件创建的组件都会被卸载】,所以最合适的地方是在组件定义的外部。

 

  4.3refs不会传递给包装组件

 

转载于:https://www.cnblogs.com/EllenChen/p/10559706.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值