I am stuck on initializing certain ui components that need Javascript, e.g, side-nav and carousel,
I tried to use React componentDIdMount()
lifecycle method, using Javascript as in materialize docs like so,
import React, { Component } from 'react';
import MenuIcon from '@material-ui/icons/Menu';
import M from 'materialize-css/dist/js/materialize.min.js';
import 'materialize-css/dist/css/materialize.min.css';
class Navbar extends Component {
componentDidMount() {
document.addEventListener('DOMContentLoaded', () => {
const elems = document.querySelectorAll('.side-nav');
const instances = M.Sidenav.init(elems);
});
}
render() {
return (
<React.Fragment>
<div className="navbar-fixed">
<nav className="teal">
<div className="container">
<div className="nav-wrapper">
<a href="#home" className="brand-logo white-text">Travellizers</a>
<a href="#!" data-target="mobile-nav" className="button-collapse hide-on-md-and-up">
<MenuIcon />
</a>
<ul className="right hide-on-med-and-down">
<li><a href="#home">Home</a></li>
<li><a href="#search">Search</a></li>
<li><a href="#popular">Popular places</a></li>
<li><a href="#about">about</a></li>
</ul>
</div>
</div>
</nav>
<ul className="side-nav" id="mobile-nav">
<li><a href="#home">Home</a></li>
<li><a href="#search">Search</a></li>
<li><a href="#popular">Popular places</a></li>
<li><a href="#about">about</a></li>
<li><a href="#contact">contact</a></li>
</ul>
</div>
</React.Fragment>
);
}
}
export default Navbar;
But I am getting an Eslint error saying instances
is assigned a value but never used. and still the side-nav isn't really working. I don't really know what am I missing.
here is my eslintrc.js
file
module.exports = {
"extends": "airbnb",
"rules": {
"react/jsx-filename-extension": [1,
{ "extensions": [".js", ".jsx"] }
],
"arrow-body-style": 0,
"linebreak-style": 0,
"no-tabs": 0,
},
"env": {
"browser": true,
},
"plugins": [
"react",
"jsx-a11y",
"import"
],
};
this is a link from materialize docs explaining how to initialize a side-nav using Javascript, so I am just trying to get that to work here. Thanks very much in advance.
Regards.
You need to do
npm install materialize-css@next
. After this, you need to import the materialize-css in your component JS file.To use the Javascript materialize-css components, a reference of these components has to be made in
componentDidMount()
and then it can be used inref
.CodeSandbox - SideNav Demo
CodeSandbox - Carousel Demo
You can check other Materialize CSS components in React from this repository - GermaVinsmoke - Reactize