开发者社区 问答 正文

#React 如何为HOC组件创建道具代理?

#React 如何为HOC组件创建道具代理?

展开
收起
因为相信,所以看见。 2020-05-07 17:01:25 948 分享 版权
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    您可以使用props代理模式添加/编辑传递给组件的props,如下所示:

    function HOC(WrappedComponent) {
      return class Test extends Component {
        render() {
          const newProps = {
            title: 'New Header',
            footer: false,
            showFeatureX: false,
            showFeatureY: true
          }
    
          return <WrappedComponent {...this.props} {...newProps} />
        }
      }
    }
    
    2020-05-07 17:02:22
    赞同 展开评论