swelet
swelet

Reputation: 8702

Receiving ALL props in a child component

Is it possible to receive ALL props passed in a child component without explicitly specifying them one by one? Eg:

const ParentComponent = () => {
  return <ChildComponent 
     prop1={"foo"}
     prop2={"bar"}
     prop3={"baz"}
  />
}

const ChildComponent = (props) => {
  return <input /*GIMME ALL PROPS HERE*/ />
}

Upvotes: 0

Views: 60

Answers (1)

hazardous
hazardous

Reputation: 10837

Use the spread operator for this -

const ChildComponent = (props) => {
    return <input {...props} />
}

This gets automatically interpreted as -

const ChildComponent = (props) => {
    return <input prop1={"foo"}
                  prop2={"bar"}
                  prop3={"baz"} />
}

Upvotes: 3

Related Questions