10

I often find myself doing something like this in my jsx:

myObject= {key1 : 'value1', key2 : 'value2'};
var reactElement = <MyReactElement key1={myObject.key1} key2={myObject.key2}/>;

Is any infrastructure provided to allow me to pass all the key value pairs in my object, as props to an element? I'm aware I can pass in the entire object, but it doesn't feel maximally elegant, especially if it's the only thing I'm passing, since I then have to refer to this.props.myObject.key1.

4

1 回答 1

18

With React 0.12 and a recent jsx version you can use the spread operator.

<MyReactElement foo={bar} {...myObject} />
于 2014-11-19T05:21:12.517 回答