Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending style object in react.js

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?

like image 212
baao Avatar asked Mar 16 '23 08:03

baao


1 Answers

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.

like image 121
Felix Kling Avatar answered Mar 26 '23 02:03

Felix Kling