【问题标题】:Ant Design: How to pass this.props.form to childrenAnt Design:如何将 this.props.form 传递给孩子
【发布时间】:2018-11-29 03:03:26
【问题描述】:

我有一个表单,每个 Form.Item 都是一个单独的组件,如下所示

  <Form>
  {this.props.children}
  </Form>

this.props.form 仅在 Form 组件中创建,如何在子组件中使用?

【问题讨论】:

    标签: reactjs antd


    【解决方案1】:

    如果您想将this.props.form 传递给孩子,您可以在您的渲染方法中执行以下操作:

    render() {
        const { children } = this.props;
    
        const childrenWithProps = React.Children.map(children, child =>
          React.cloneElement(child, { someForm: this.props.form })
        );
    
        return <Form>{childrenWithProps}</Form>
    }
    

    然后在您的子组件中,您可以通过this.props.someForm 访问该道具,例如。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      • 2020-08-08
      相关资源
      最近更新 更多