Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use environment variables in ReactJS imported CSS?

I created a simple React app using https://github.com/facebookincubator/create-react-app.

I am trying to specify a configurable URL to a component's CSS like this:

.Cell {
    background-image: url({process.env.REACT_APP_STATIC_URL}./someImage.png);
}

And here's the component:

import React from 'react';
import './Cell.css'

class Cell extends React.Component {
    render() {
        return (
            <div className="Cell">
                {this.props.name}
            </div>
        );
    }
}

But it doesn't look like the process variable trickles down to the imported CSS. How would I go about doing this?

like image 602
Ben Avatar asked Oct 18 '22 00:10

Ben


1 Answers

If you want to use js variables in CSS, try React inline-style instead of plain css file.

https://facebook.github.io/react/tips/inline-styles.html

If you want to separate CSS and JS, you might need a CSS preprocessor to manage your global variables.

like image 99
Jean Y.C. Yang Avatar answered Oct 21 '22 00:10

Jean Y.C. Yang