Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React component with two sets of children

I'm creating a component that needs to take in two sets of children and to be placed in two different parts of a component.

let CreditCardForm = ({icons, fields}) => (
  <div>
    <div className='some'>
      <div className='special'>
        <div className='nesting'>
          {icons}
        </div>
      </div>
    </div>
    {fields}
  </div>
)

let CreditCardFormUsage = () => {
  let icons = () => (
    <Icons>
      <IconBogus/>
      <IconVisa/>
      <IconPaypal/>
      <IconMore/>
    </Icons>
  )

  let fields = () => (
    <CreditCardFields>
      <FieldCardNumber/>
      <FieldName/>
      <FieldExpirey/>
      <FieldCCV/>
    </CreditCardFields>
  )
  return (
    <CreditCardForm icons={icons} fields={fields}/>
  )
}

The code above should work, my question is it possible to grab those property values based on the children in the element itself, and have something more natural?

<CreditCardForm>
  <Icons>
    <IconBogus/>
    <IconVisa/>
    <IconPaypal/>
    <IconMore/>
  </Icons>
  <CreditCardFields>
    <FieldCardNumber/>
    <FieldName/>
    <FieldExpirey/>
    <FieldCCV/>
  </CreditCardFields>
</CreditCardForm>
like image 521
ThomasReggi Avatar asked Feb 03 '16 15:02

ThomasReggi


People also ask

How do you pass multiple children in React?

const Child = (props) => { return <p>{props. text}</p> }; const Parent = (props) => { // children is a property on every React component's props and // refers to the React components nested within the Parent component return <div> {props.

Can we render two child components in React?

React allows us to render one component inside another component. It means, we can create the parent-child relationship between the 2 or more components. Prerequisites: The pre-requisites for this project are: React Knowledge.

Can React components have children?

In React, a component can have one, many, or no children.

Can a child component have multiple parents?

A child component must have one parent at any one time. You can remove a child component from its parent and then add it to another parent if you wish.


2 Answers

Yes, this.props.children will return an array so if you always want to load specific children, then just reference those children by index in your wrapper. Then you could just turn icons and fields into wrapper components. Here is a working jsfiddle. See how the render method of App is exactly what you want.

CreditCardForm render:

<div>
    <div className='some'>
      <div className='special'>
        <div className='nesting'>
          {this.props.children[0]}
        </div>
      </div>
    </div>
    {this.props.children[1]}
 </div>

Fields and Icons render:

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

App render:

<CreditCardForm>
    <Icons>
        <IconBogus />
    </Icons>
    <Fields>
      <FieldCardNumber />
      <FieldName />
    </Fields>
</CreditCardForm>
like image 167
Andy Noelker Avatar answered Sep 28 '22 07:09

Andy Noelker


yes, you can do it with child props. Read more @docs:

https://facebook.github.io/react/tips/children-props-type.html

And of course check out React.Children

https://facebook.github.io/react/docs/top-level-api.html#react.children

like image 40
Lukas Liesis Avatar answered Sep 28 '22 05:09

Lukas Liesis