Original answer
In case you are using Webpack, you have the raw-loader to import files and being resolved in your build as a string.
Considering that you want to process every .css file in your project. Just add a rule in your Webpack configuration:
{
test: /\.css$/i,
use: 'raw-loader',
}
In case you only want to do this for just one file, you can specify the loader you want to use for that file when you are importing it:
import css from 'raw-loader!./styles.css';
class App extends Component {
componentDidMount() {
console.log(css);
}
}
Edit
I'm going to edit my answer because it won't work for you, but the original answer could come in handy for others.
First of all, I'm missing what's the reason why you may want to send the raw css to a back-end and why you want to do this from a React component. Your css file is being processed by the css-modules loader, so you won't be able to get the raw css because that's not the purpose of css-modules. What you will get instead, it's an object with the naming references generated by this loader.
I searched if there is any way to get also the styles and I couldn't find anything because css-modules is not intended to do that. What you can do instead to get the css files is getting it directly from the build output. Since you are using create-react-app that would be in the /build folder in the root of your directory.
In case you really need to do this in the component, then you should rename your file to style.css without the .module.css extension. The create-react-app would process your file with the post-css loader instead, which will allow you to get the css.
You still would need to preprocess your css with the raw-loader and since you don't have complete control over the create-react-app build and they have a strict linter to throw exceptions if anyone tries to do this. You would need to disable the linter for that line and import the css with the raw-loader instead.
/* eslint import/no-webpack-loader-syntax: off */
import css from '!!raw-loader!./styles.css';
class App extends Component {
componentDidMount() {
console.log(css);
}
}
Hope this could help you, though it's just a workaround and it's not recommended.
css-modulesso what you get from importing that file it's an object with the naming references generated by this loader in the Webpack build. It's also important to mention that since you are usingcreate-react-appyou don't have full control over the build automation.