1

I'm currently working with rails and reactjs. I'm having difficulties using css in my reactjs files. It seems like every time i try to use it, no change is being applied at all. In my App.jsx file I have this:

import React from "react";
import styles from "./styles.css";

export default class Register extends React.Component {

  render() {
    return (
      <div className={styles.container}>
        <h1> this text should appear to the right </h1>
      </div>
   );
  }
}

And in my styles.css file I have this:

.container {
    width:40%;
    text-align:right;
}  

For the record I am using webpack. Can anyone help me understand why the css isn't having any effect on my jsx components. I've looked all over for help but was unable to put the pieces together.

If it matters, this is how my "config/webpack/development.js" file looks like:

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()
2
  • Are you using the ExtractTextPlugin in dev mode (which you shouldn't be) and not including a style tag pointing to the built css file? And what does "isn't having any effect" mean? Do you see the styles in the chrome inspector for that rendered element? Commented Sep 23, 2018 at 19:31
  • @AndyRay when i look in the firefox inspector it shows: <div class = "someRandomString" followed by the h1 header i originally had Commented Sep 23, 2018 at 19:49

2 Answers 2

1

It depends on the webpack loader settings. If you are using css-loader as configured in react-scripts (as of 1.1.5), then the classNames are loaded using {modules: false} option, i.e. global styles, which can be referenced as strings in JSX code:

import "./styles.css";
... className="container" ...

Or you can load local styles using following CSS-file syntax:

:local .container {...

Or edit your webpack.config.js appropriately (see https://github.com/webpack-contrib/css-loader#scope for the official documentation of various options).

Sign up to request clarification or add additional context in comments.

3 Comments

Where can I find the webpack.config.js. In terms of webpack, all i can see is my webpack.yml inside config and the development.js, production.js, environment.js and test.js inside config/webpack. Inside my original question, I added how the development.js looks like
webpack.config.js is explained here: webpack.js.org/configuration - maybe try searching for css-loader in your files to see if you can find where it's configured... if not, read the documentation from the people who configured how to configure your project...
all modules seem to be automatically set to true in webpack.config.js
0

seems like you didn't enable an option { modules: true } for css-loader in webpack config

take a look

webpack-contrib/sass-loader#206

https://github.com/webpack-contrib/css-loader#options

Taken from: https://github.com/facebook/create-react-app/issues/1350

3 Comments

Where can i set the {modules: true}
The modules are set to true and its still not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.