-
Notifications
You must be signed in to change notification settings - Fork 868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to include styling in a component-like way #439
Comments
Hi If you are using Webpack (I assume you are as you're using css-loader), you should be able to require/import You can also import from within an SCSS file using I'm not happy about this in my project, so to reduce the dependencies on this date component, I am adding a helper DateTime component to my project that is just a wrapper for react-datetime and has the css import at the top of the js. So if I replace react-datetime or just want to change the default params for my project etc, I only have to update the one file. Something like:
If still having trouble, please post what build system you're using and a snippet of how you're importing the css and where. Cheers, |
So you're adding the wrapper in case you want to switch out this DateTime for another later on? Not sure if I understood that correctly. So here's what my code looks like, styling is not applied. import Datetime from 'react-datetime';
import 'react-datetime/css/react-datetime.css'
export default class EventSearchForm extends React.Component {
render() {
return (
<div>
<Datetime/>
</div>
);
} If I instead use import styles from 'react-datetime/css/react-datetime.css'
...
<Datetime className={styles.rdt} /> No improvements are made. Am I doing this wrong, or is there probably an issue with my Webpack/CSS loading dependencies not working? |
It seems like its a problem with your webpack setup. Are you including other css/scss files ok or are you using some CSS in JS system? Cheers, |
I am using Webpacker 3.0 in a Rails 5.1 App. Just tried to integrate this Webpack configuration for my dev environment.js: rails/webpacker#756 . Unfortunately that doesn't seem to be working. I am using styled-components but also have css-loader installed locally. My package.json: {
"name": "asdf",
"private": true,
"dependencies": {
"@rails/webpacker": "^3.0.1",
"moment": "^2.18.1",
"prop-types": "^15.6.0",
"react": "^16.0.0",
"react-datetime": "^2.10.3",
"react-dom": "^16.0.0",
"react-google-maps": "^8.3.4",
"styled-components": "^2.2.1"
},
"devDependencies": {
"babel-plugin-styled-components": "^1.2.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.7",
"webpack-dev-server": "^2.9.1",
"webpack-merge": "^4.1.0"
}
} |
It was an issue with my Rails setup. Got the loader working here- rails/webpacker#775 (comment). Now the issue is that import styles from 'react-datetime/css/react-datetime.css'
...
<Datetime className={styles.rdt} /> does work but only on the top-level |
From your list of dependencies, I think the problem is your webpack config's CSS module rule. You normally use
This Stack Overflow post explains it well:
You can also add sass-loader to the end of the css loaders and update the test to Instead of using If you are using some other CSS in JS library, they may have some other way to import static styles. Cheers, |
I'm learning this stuff so I had no idea there was a style loader too, thanks for the info + links! Is there anything I need to do to make After checking I think style loader is already included. I believe the way it works in Rails is that it has default NPM dependencies and then you can add additional ones in package.json. When I print out the default 'style' loaders this is what I receive.
|
Ok, as you said I suggest using Facebook's https://github.com/facebookincubator/create-react-app to create an example app already setup to play around with config settings and compare with your own app.
I still recommend you just use style-loader if you can get it to work. Also, if you do use ExtractTextWebpackPlugin, it should only be used for your production build as it interferes with Webpack hot reloading of changed css files - this will slow down your development. ExtractTextWebpackPlugin has a disable property that you can use to turn it on or off but it can be easier to have separate Webpack configs, one for debug (with it removed altogether) and one for production, and run webpack like This is my config that I simplified a bit for your reference. I haven't used Webpack in Rails so the above might not apply to you. I suggest you add an issue to https://github.com/rails/webpacker/issues and ask them for help adding this CSS file. Good luck getting style-loader working!
|
I'm Submitting a ...
Other than including the CSS globally in the head of the HTML document, how can we getting working in a react/component like way? I've tried using css-loader and styled-components without much luck.
The text was updated successfully, but these errors were encountered: