我有一个已登录的应用程序,用户具有sidenav,该信息来自Materialize CSS库。
这里是反应成分
import React, {Fragment, useEffect} from 'react'
import {Link} from 'react-router-dom'
// Materialize JS Import
import M from 'materialize-css/dist/js/materialize.min.js'
import "materialize-css/dist/css/materialize.min.css"
// Actions
import {logout, loadUser} from '../../actions/authActions'
const navbarStyles = {
marginLeft: '20px'
}
const Navbar = ({loadUser, logout, auth: {isAuthenticated, loading}}) => {
useEffect(() => {
loadUser()
if(isAuthenticated !== null) {
var elems = document.querySelectorAll('.sidenav');
var instances = M.Sidenav.init(elems);
}
}, [isAuthenticated])
const onLogout = () => {
logout()
}
return (
<Fragment>
{isAuthenticated && !loading ? (
<Fragment>
<div className="navbar-fixed">
<nav id="nav">
<div className="nav-wrapper teal accent-4">
<Link to="/"><a href="#" className="brand-logo" style={navbarStyles}>Home</a></Link>
<a href="#" data-target="sidenav-overlay" className="sidenav-trigger"><i className="material-icons">menu</i></a>
<ul id="nav-mobile" className="right hide-on-med-and-down">
<li><Link to="/upload" className="waves-effect waves-dark" ><i className="small material-icons left">cloud</i>Upload</Link></li>
<li><Link to="/guests/list" className="waves-effect waves-dark" ><i className="small material-icons left">list</i>Guest List</Link></li>
<li><Link to="/apartments" className="waves-effect waves-dark" ><i className="small material-icons left">hotel</i>Apartments</Link></li>
<li><Link to="/keys" className="waves-effect waves-dark" ><i className="small material-icons left">vpn_key</i>Keys</Link></li>
<li><Link to="/logs" className="waves-effect waves-dark" ><i className="small material-icons left">history</i>Logs</Link></li>
<li><a href="#!" className="waves-effect waves-dark" onClick={onLogout}><i className="small material-icons left">power_settings_new</i>Logout</a></li>
</ul>
</div>
</nav>
</div>
<ul className="sidenav" id="sidenav-overlay">
<li><Link to="/upload" className="waves-effect waves-dark sidenav-close" ><i className="small material-icons left">cloud</i>Upload</Link></li>
<li><Link to="/guests/list" className="waves-effect waves-dark sidenav-close" ><i className="small material-icons left">list</i>Guest List</Link></li>
<li><Link to="/apartments" className="waves-effect waves-dark sidenav-close" ><i className="small material-icons left">hotel</i>Apartments</Link></li>
<li><Link to="/keys" className="waves-effect waves-dark sidenav-close" ><i className="small material-icons left">vpn_key</i>Keys</Link></li>
<li><Link to="/logs" className="waves-effect waves-dark sidenav-close" ><i className="small material-icons left">history</i>Logs</Link></li>
<li><a href="#!" className="waves-effect waves-dark sidenav-close" onClick={onLogout}><i className="small material-icons left">power_settings_new</i>Logout</a></li>
</ul>
</Fragment>
) : (
<div className="navbar-fixed">
<nav id="nav">
<div className="nav-wrapper teal accent-4">
<Link to="/"><a href="#" className="brand-logo" style={navbarStyles}>Home</a></Link>
</div>
</nav>
</div>
)}
</Fragment>
)
}
export default Navbar
我已经检查了Materialise实际上将Sidenav初始化为元素,但是触发器不起作用。另外,如果我调用实例的.open()方法,则应用程序只会变得暗淡(sidenav效果,但nav没有显示)。
[此外,我在其他组件中使用Materialize的方式相同,但在其他功能中,它们运行良好。仅Sidenav部件崩溃。
import React from 'react';
import clsx from 'clsx';
import { makeStyles } from '@material-ui/core/styles';
import Drawer from '@material-ui/core/Drawer';
import Button from '@material-ui/core/Button';
import List from '@material-ui/core/List';
import Divider from '@material-ui/core/Divider';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import InboxIcon from '@material-ui/icons/MoveToInbox';
import MailIcon from '@material-ui/icons/Mail';
const useStyles = makeStyles({
list: {
width: 250,
},
fullList: {
width: 'auto',
},
});
export default function Navbar() {
const classes = useStyles();
const [state, setState] = React.useState({
top: false,
left: false,
bottom: false,
right: false,
});
const toggleDrawer = (anchor, open) => (event) => {
if (event.type === 'keydown' && (event.key === 'Tab' || event.key === 'Shift')) {
return;
}
setState({ ...state, [anchor]: open });
};
const list = (anchor) => (
<div
className={clsx(classes.list, {
[classes.fullList]: anchor === 'top' || anchor === 'bottom',
})}
role="presentation"
onClick={toggleDrawer(anchor, false)}
onKeyDown={toggleDrawer(anchor, false)}
>
<List>
{['Inbox', 'Starred', 'Send email', 'Drafts'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
<List>
{['All mail', 'Trash', 'Spam'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
</div>
);
return (
<div>
{['left', 'right', 'top', 'bottom'].map((anchor) => (
<React.Fragment key={anchor}>
<Button onClick={toggleDrawer(anchor, true)}>{anchor}</Button>
<Drawer anchor={anchor} open={state[anchor]} onClose={toggleDrawer(anchor, false)}>
{list(anchor)}
</Drawer>
</React.Fragment>
))}
</div>
);
}