FEi.TH
FEi.TH

Reputation: 101

Ant Design: How to pass this.props.form to children

I have a form and each Form.Item is a separate componenent, looks like the below

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

How can I use this.props.form in the children since it is only created in Form component?

Upvotes: 1

Views: 2403

Answers (1)

Shawn Andrews
Shawn Andrews

Reputation: 1442

You can do the following in your render method if you wanted to pass down this.props.form to the children:

render() {
    const { children } = this.props;

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

    return <Form>{childrenWithProps}</Form>
}

Then in your children component you can access the prop via this.props.someForm, for example.

Upvotes: 1

Related Questions