I am trying to use the MUI drawer component alongside with React router to display a simple navbar on the left of the page. I am very new to react and am not sure what I am doing wrong as the code compiles fine. The other two pages “Signup” and “Login” are showing up fine. The way the project is structured is I have a separate folder in Components for each page of the website. Then in app.js there is a router which should have a link to each page. I do not have components separated and am trying to have a Drawer as well as other components all in one page titled Dashboard.js
JavaScript
x
48
48
1
import './App.css';
2
3
//Importing Components
4
import Login from "./Components/Login/Login.js";
5
import Signup from "./Components/SignUp/Signup.js";
6
import Dashboard from "./Components/Dashboard/Dashboard.js";
7
8
function App() {
9
return (
10
11
<BrowserRouter>
12
<Routes>
13
<Route
14
path="/login"
15
element={
16
<div className="App">
17
<Login/>
18
</div>
19
20
}
21
/>
22
<Route
23
path="/dashboard"
24
element={
25
<div className="App">
26
<Dashboard/>
27
</div>
28
29
}
30
/>
31
<Route
32
path="/Signup"
33
element={
34
<div className="App">
35
36
<Signup/>
37
38
</div>
39
}
40
/>
41
</Routes>
42
</BrowserRouter>
43
44
);
45
46
}
47
export default App;
48
JavaScript
1
46
46
1
import {Drawer, Box, Typography, IconButton} from "@mui/material";
2
import MenuIcon from "@mui/icons-material/Menu";
3
import {useState} from 'react';
4
5
6
const Dashboard = () => {
7
const [isDrawerOpen, setIsDrawerOpen] = useState(false)
8
return (
9
<>
10
<IconButton
11
size = 'large'
12
edge = 'start'
13
color = 'inherit'
14
aria-label = 'logo'
15
onCLick = {() => setIsDrawerOpen(true)}
16
>
17
<MenuIcon/>
18
</IconButton>
19
20
<Drawer
21
anchor = 'left'
22
open = {isDrawerOpen}
23
onClose={() => setIsDrawerOpen(false)}>
24
25
<Box
26
p={2}
27
width = '250px'
28
textAlign = 'center'
29
role= 'presentation'
30
>
31
32
<Typography
33
variant = 'h6'
34
component ='div'>
35
36
</Typography>
37
</Box>
38
</Drawer>
39
</>
40
)
41
42
43
}
44
export default Dashboard;
45
46
Advertisement
Answer
Change your code to this
JavaScript
1
15
15
1
const handleDrawerOpen = () => {
2
setIsDrawerOpen(true);
3
};
4
5
6
<IconButton
7
size="large"
8
edge="start"
9
color="inherit"
10
aria-label="logo"
11
onClick={handleDrawerOpen}
12
>
13
<MenuIcon />
14
</IconButton>
15
I created a codesandbox from your code and onClick works there