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
.
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
.
1 Answer
Reset to default 19With React 0.12 and a recent jsx version you can use the spread operator.
<MyReactElement foo={bar} {...myObject} />