Ilja
Ilja

Reputation: 46479

Add props to ...this.props

I'm passing down a list of props to children components like {...this.props} however have encountered a case where I need to add additional props, I believe I can do it like <Child {...this.props} addProp="myNewProp" />, however to keep code cleaner, can I somehow add addProp to the list of ...this.props, so I only pass it down?

Upvotes: 1

Views: 149

Answers (2)

Nikolai Mavrenkov
Nikolai Mavrenkov

Reputation: 1923

Try this:

<Child {...{addProp:"myNewProp", ...this.props}}/>

(but, in my opinion, usual JSX attribute syntax looks better it your example)

Also, you should remember that although this syntax supported by Babel's plugin, it is still a proposal and could not be included in actual ES7.

Upvotes: 1

iofjuupasli
iofjuupasli

Reputation: 3873

You can use JavaScript:

h(Child, {
    ...this.props,
    addProp: "myNewProp"
})

h is just alias: const h = React.createElement or JSnoX

Upvotes: 0

Related Questions