Skip to content
Advertisement

Combining CSS and custom CSS properties inline on a react component

I need to pass custom CSS properties inline to a React component (Google Model Viewer in this instance),combined with more run-of-the-mill styles (background width, height etc.). There’s quite a lot of questions on using custom properties inline with react, but I couldn’t see one for specifically combing the properties with other styles.

I have the styles here, with the commented out style being an example of the custom property.

JavaScript

The const gets passed into rendering the component:

JavaScript

How do I combine the usual inline styles with the custom properties to add inline?

I tried adding it as a variable (as seen elsewhere on SO):

JavaScript

But wasn’t sure how to combine that with the {styles} . I tried longhand declaring each style and adding the variable to the list, but that didnt work either:

JavaScript

Any tips?

Advertisement

Answer

You can simply use object spreading for this

JavaScript

Here’s a working demo

Advertisement