I'm new to Material UI and ReactJS. I've been playing around with Create-React-App (CRA) and reactstrap. My question is, can I use Material UI and CRA together to build an app?
1 Answer
First install material-ui
npm install --save material-ui
or
yarn add material-ui
src/App.js
import React, { Component } from 'react';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'; // add
import RaisedButton from 'material-ui/RaisedButton'; // add
import logo from './logo.svg';
import './App.css';
class App extends Component {
render() {
return (
<MuiThemeProvider>
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
<RaisedButton label="Material UI" />
</div>
</MuiThemeProvider>
);
}
}
export default App;
In summary, these are the things to be done:
Install necessary packages.
import
MuiThemeProvidercomponent inApp.js, Enclose the app div in MuiThemeProvider component- Now you can import and use any component in
material-uilike I used RaisedButton here
Versions of material-ui before 0.19.1 required react-tap-event-plugin
For those versions, you had to do make this change in index.js
src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import injectTapEventPlugin from 'react-tap-event-plugin'; // add
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
injectTapEventPlugin(); // add
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
- import
injectTapEventPluginfromreact-tap-event-plugininindex.jsand initialise it.injectTapEventPluginis used inorder to remove tap delay in iOS.
5 Comments
wobsoriano
Thanks for this. Is there a way that all components can be used without importing them one-by-one?
Will Luce
That's a fairly frequent question from people getting started. Fundamentally, JS's module paradigm is rooted in segregated, reusable components. They aren't part of a larger ecosystem that can be imported all at once. It's a fairly large topic to get into here, but, in short, no you can't import all of Material-UI in one statement.
WorldSEnder
The reason for
react-tap-event-plugin is just as a workaround for an iOS browser issue and not essential to the problem, right?MattD
According to the official NPM page,
react-tap-event-plugin isn't required if you're using version 0.19.0 or higher. npmjs.com/package/material-ui#react-tap-event-pluginSvet Angelov
The current documentation (Sept. 2019) shows to install using: npm install --save @material-ui/core . Using the above was giving me errors when trying the material ui examples