JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

Combining multiple inline style objects in React

Author:JIYIK Last Updated:2025/03/16 Views:

Use spread syntax to combine multiple inline style objects in React, for example style={{...style1, ...style2}}. The spread syntax will unpack the key-value pairs of the object into a final object and the styles will be applied to the element.

export default function App() {
  const style1 = {backgroundColor: 'salmon'};
  const style2 = {fontSize: '2rem'};

  return (
    <div>
      <div style={{...style1, ...style2}}>Some content here</div>
    </div>
  );
}

We use spread syntax ...to combine multiple inline style objects in React.

We can merge as many inline style objects as needed.

export default function App() {
  const style1 = {backgroundColor: 'salmon'};
  const style2 = {fontSize: '2rem'};
  const style3 = {color: 'white'};

  return (
    <div>
      <div style={{...style1, ...style2, ...style3}}>Some content here</div>
    </div>
  );
}

We can also add key-value pairs inline after composing the style object.

export default function App() {
  const style1 = {backgroundColor: 'salmon'};
  const style2 = {fontSize: '2rem'};

  return (
    <div>
      <div style={{...style1, ...style2, color: 'white', padding: '2rem'}}>
        Some content here
      </div>
    </div>
  );
}

An easy way to think about spread syntax ...is that we are unpacking the key-value pairs of an object into a new object.

请注意, we styleuse 2 sets of curly braces for the prop. The outer set of curly braces marks the beginning of the expression that will be evaluated.

The inner set of curly braces is an object containing styles and values.

It is important to note that the order in which style objects are combined using the spread syntax is important.

export default function App() {
  const style1 = {backgroundColor: 'salmon'};
  const style2 = {fontSize: '2rem'};
  const style3 = {backgroundColor: 'blue'}; // 👈️ overrides style1

  return (
    <div>
      <div style={{...style1, ...style2, ...style3}}>Some content here</div>
    </div>
  );
}

请注意, objects style1 and style3 set backgroundColorthe property to different values.

If two objects have the same key, the object whose properties are unpacked later wins.

Both objects have a backgroundColor property, but style3's key is unpacked later, so its value overwrites the value of the backgroundColor property in style1.

We may also see examples online Object.assignof using the method to combine inline style objects.

export default function App() {
  const style1 = {backgroundColor: 'salmon'};
  const style2 = {fontSize: '2rem'};

  return (
    <div>
      <div style={Object.assign({}, style1, style2, {color: 'white'})}>
        Some content here
      </div>
    </div>
  );
}

Object.assign()The first argument the method takes is the target object—the object to which the properties of the source object are applied.

The next parameter the method takes is one or more source objects.

The properties of the target object are overwritten by other objects with the same properties in the argument order.

export default function App() {
  const style1 = {backgroundColor: 'salmon'};
  const style2 = {fontSize: '2rem'};

  return (
    <div>
      <div style={Object.assign({}, style1, style2, {backgroundColor: 'lime'})}>
        Some content here
      </div>
    </div>
  );
}

This behavior ...is consistent with the extended syntax.

Either of these two approaches will work, but I’ll stick with the spread syntax ...as I find it easier to read and more commonly used in React.js applications.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:185 Category:React

React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.

React Tutorial: Props Anti-Pattern

Publish Date:2025/03/16 Views:183 Category:React

React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.

React Tutorial - Props Validation

Publish Date:2025/03/16 Views:99 Category:React

Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:58 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:187 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Solve the Module not found: Can't resolve 'react-bootstrap' error

Publish Date:2025/03/16 Views:85 Category:React

To resolve the error "Module not found: Error: Can't resolve 'react-bootstrap'", make sure to install the react-bootstrap package by opening a terminal in the root directory of the project and running the command `npm install react-bootstrap bootstrap

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial