I have a Meteor/React project, using ES6 modules. I've installed materialize-css using npm, but I'm not sure how to actually use the Materialize classes in my JSX code. What am I supposed to import from materialize-css? Or do I just have to include the CSS in my main index.html file?
I mostly want it for the grid system, as I'll be using material-ui for the actual UI components.
With NPM:
Step 1: Install materialize
If you are using npm, make sure you install materialize using the command listed in their documentation:
DON'T MISS the '@next' at the end. The installed version will be something like: "^1.0.0-rc.2" OR "^1.0.0-alpha.4"
Step 2: Import materialize:
OR
In order for the css import to work, you would need a css loader. Note that this loader is already included in projects built using create-react-app so you don't need the next steps. If instead, you are using custom webpack config, then run:
Now add css-loader and style-loader in webpack config
Now you can initialize components individually, or all at once using M.AutoInit();
With CDN:
Add the following in your HTML file.
Then, in the webpack config, add externals: https://webpack.js.org/configuration/externals/
These answers didn't satisfy my biggest concern which was bundle size and importing a ton of code in order to use a few components. I've written up a solution here that includes code splitting and an easy compilation step.
The key points are:
Read post for more details.
Since I use CSS Modules, importing materialize css would scope it to that particular component. So I did the following
Step 1) install materialise
Step 2) in index.html
Step 3) import materialise.js in whichever component its needed
for e.g. in SomeComponent.js (for e.g. if this is about a sidenav)
just a beginner, so I would appreciate any comments on downsides of this method
There are possible ways that I can recommend to use:
One way is just include your stylesheet file in index.html and use className property in your React components just like this.
Another way is to bundle all your stylesheeets in one stylesheet file and to use them as previous one.
One option could be to use webpack. By using webpack, it is possible to use embedded stylesheets in jsx files just by requiring stylesheet that you want to include.
To examine in detail webpack stylesheet option: http://webpack.github.io/docs/stylesheets.html
You can use https://react-materialize.github.io/#/, why to reinvent the wheel.
installation
react-materialize
Usage
Sample
https://github.com/hiteshsahu/react-materializecss-template
Screenshot
You can copy into "imports" folder and add by
or use this for LESS example