最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React.js pass object key-value pairs as props - Stack Overflow

programmeradmin0浏览0评论

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.

Share Improve this question edited Mar 8, 2015 at 13:52 eye_mew asked Nov 19, 2014 at 4:41 eye_meweye_mew 9,1337 gold badges34 silver badges52 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 19

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

<MyReactElement foo={bar} {...myObject} />
发布评论

评论列表(0)

  1. 暂无评论