I'm developing an app with React, I want to use the sidenav component http://materializecss.com/side-nav.html
The problem is that I'm getting this error:
Sidebar.js:8 Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_2_jquery___default(...)(...).sideNav is not a function
at HTMLDocument.<anonymous> (Sidebar.js:8)
at mightThrow (jquery.js:3534)
at process (jquery.js:3602)
I'm so confused with the way of import components, libraries. There are a lot of them and a lot of ways to configure it.
What I've done is:
Create the app with
create-react-app
Install materialize via npm
npm install materialize-css@next
Create the react app
Here's the code I'm using:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/babel" src="js/materialize.min.js"></script>
</body>
</html>
App.js
import React from 'react'
import {BrowserRouter, Route} from 'react-router-dom';
import Sidebar from './Sidebar'
const Dashboard = () => <h2>Dashboard</h2>
const Survey = () => <h2>Survey</h2>
const Landing = () => <h2>Landing</h2>
const Home = () => <h2>Home</h2>
const App = () => {
return (
<div>
<BrowserRouter>
<div>
<Sidebar />
<Route exact path="/" component={Home} />
<Route path="/surveys" component={Dashboard} />
</div>
</BrowserRouter>
Hi There!
</div>
)
}
export default App;
Sidebar.js
import React, {Component} from 'react';
import 'materialize-css/dist/css/materialize.min.css';
import $ from 'jquery';
class Sidebar extends Component {
componentDidMount() {
$(document).ready(function(){
$('.sidenav').sideNav();
});
}
render() {
return (
<ul id="slide-out" className="sidenav">
<li><a href="#!"><i className="material-icons">cloud</i>First Link With Icon</a></li>
<li><a href="#!">Second Link</a></li>
</ul>
);
}
}
export default Sidebar;
package.json
{
"name": "support-tools",
"version": "0.1.0",
"private": true,
"dependencies": {
"jquery": "^3.3.1",
"materialize-css": "^1.0.0-alpha.4",
"nodemon": "^1.15.1",
"react": "^16.2.0",
"react-bootstrap": "^0.32.1",
"react-bootstrap-sidebar": "0.0.1",
"react-burger-menu": "^2.2.3",
"react-dom": "^16.2.0",
"react-materialize": "^1.1.2",
"react-mdl": "^1.11.0",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"react-scripts": "1.1.1",
"react-sidebar": "^2.3.2",
"react-sidenav": "^2.1.2",
"redux": "^3.7.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
I've tried use two versions of jQuery on index.js
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
and
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
But I can have the sidenav running. What could I do?
After many tries finally I got running the SideNav running with React:
package.json
{
"name": "sidebar",
"version": "0.1.0",
"private": true,
"dependencies": {
"jquery": "^3.3.1",
"material-design-icons": "^3.0.1",
"materialize-css": "^1.0.0-alpha.4",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-mdl": "^1.11.0",
"react-scripts": "1.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<title>React App</title>
<style type="text/css">
select{
display: block !Important;
}
</style>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</body>
</html>
App.js
import React from 'react'
import {BrowserRouter, Route} from 'react-router-dom';
import Sidebar from './Sidebar'
const Dashboard = () => <h2>Dashboard</h2>
const Survey = () => <h2>Survey</h2>
const Landing = () => <h2>Landing</h2>
const Home = () => <h2>Home</h2>
const App = () => {
return (
<div>
<BrowserRouter>
<div>
<Sidebar />
<Route exact path="/" component={Home} />
<Route path="/surveys" component={Dashboard} />
</div>
</BrowserRouter>
Hi There!
</div>
)
}
export default App;
Sidebar.js
import React, { Component } from "react";
import M from "materialize-css/dist/js/materialize.min.js";
import { BrowserRouter, Route } from "react-router-dom";
import "materialize-css/dist/css/materialize.min.css";
class Sidebar extends Component {
componentDidMount() {
var elem = document.querySelector(".sidenav");
var instance = M.Sidenav.init(elem, {
edge: "left",
inDuration: 250
});
}
render() {
return (
<div>
<ul id="slide-out" className="sidenav">
<li />
<li>
<a href="#!">
<i className="material-icons">cloud</i>First Link
With Icon
</a>
</li>
<li>
<a href="#!">Second Link</a>
</li>
<li>
<div className="divider" />
</li>
<li>
<a className="subheader">Subheader</a>
</li>
<li>
<a className="waves-effect" href="#!">
Third Link With Waves
</a>
</li>
</ul>
<a href="#" data-target="slide-out" className="sidenav-trigger">
<i className="material-icons">menu</i>
</a>
</div>
);
}
}
export default Sidebar;
Also you can checkout the project from here:
https://github.com/AndyBraveMX/react-learning/tree/master/react-sidenav-materializecss
Just wanted to point out for other users, if you are using npm, make sure you install materialize using the command listed in their documentation: npm install materialize-css@next
Don't forget the '@next' at the end.
The installed version will be something like:
"materialize-css": "^1.0.0-rc.2" OR "materialize-css": "^1.0.0-alpha.4"
https://materializecss.com/getting-started.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With