美文网首页
React高阶组件HOC

React高阶组件HOC

作者: GC风暴 | 来源:发表于2019-12-20 11:29 被阅读0次

    高阶组件本质是函数,参数是 组件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

    相关文章

      网友评论

          本文标题:React高阶组件HOC

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