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

javascript - Extending style object in react.js - Stack Overflow

programmeradmin3浏览0评论

I'm using react.js and manage most of the style attributes via react, too. If I have a, for example, a success and an error button I want to apply a different background color to them.

Right now, I'm doing this with jQuery's extend function:

<span style={$.extend({background: 'forestgreen'},this.state.buttonStyle)}></span>

But I'm wondering if there is a better and maybe more readable solution for this. Any advice?

I'm using react.js and manage most of the style attributes via react, too. If I have a, for example, a success and an error button I want to apply a different background color to them.

Right now, I'm doing this with jQuery's extend function:

<span style={$.extend({background: 'forestgreen'},this.state.buttonStyle)}></span>

But I'm wondering if there is a better and maybe more readable solution for this. Any advice?

Share Improve this question asked Jul 22, 2015 at 16:14 baaobaao 73.4k18 gold badges150 silver badges207 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

If you use a transpiler that implements the spread property proposal (such as Babel), you can write

style={{...this.state.buttonStyle, background: 'forestgreen'}}

instead.

Whether thats "better" or more readable is likely subjective.

发布评论

评论列表(0)

  1. 暂无评论