Reputation: 4388
I am pretty new to react and I am now trying to use React Router and Material UI together.
I have my Layout file which uses Navigation drawer from material UI and I have placed my menu on the left and the content on the right as shown in the pic below
What I want to do is, when I click on the Test Comp
it should load the TestComp.js
component on the right and so on for Test Comp One
to load TestCompOne.js
.
I have been through the ReactRouter code with Links, Switch and Route but I am not exactly sure where to place them.
I tried adding the following code for my Layout file which should have the Router but I am sure I have not done it right Error I get is
Error: Invariant failed: You should not use <Link> outside a <Router>
could you please correct me to get this right please
index.js
import Layout from './Layout/Layout'
ReactDOM.render(
<React.StrictMode>
<Layout/>
</React.StrictMode>,
document.getElementById('root')
);
component - TestComp.js
import React from "react"
export default class TestComp extends React.Component {
render() {
return (
<h1>test component for test</h1>
);
}
}
Component TestCompOne.js
import React from "react"
export default class TestCompOne extends React.Component {
render() {
return (
<h1>test component ONE for test</h1>
);
}
}
I am pasting the entire Layout file which is a bit big but the main area is the drawer
which is the left menu and content to be displayed is where I added the <Router>
.
Apologies if this is completely wrong way of doing, please suggest
Component Layout.js - which has the Navigation drawer from material UI and where I want to add Router to open individual components
import React from 'react';
import PropTypes from 'prop-types';
import AppBar from '@material-ui/core/AppBar';
import CssBaseline from '@material-ui/core/CssBaseline';
import Divider from '@material-ui/core/Divider';
import Drawer from '@material-ui/core/Drawer';
import Hidden from '@material-ui/core/Hidden';
import IconButton from '@material-ui/core/IconButton';
import InboxIcon from '@material-ui/icons/MoveToInbox';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import MailIcon from '@material-ui/icons/Mail';
import MenuIcon from '@material-ui/icons/Menu';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import { makeStyles, useTheme } from '@material-ui/core/styles';
import { MenuItem, MenuList } from '@material-ui/core';
import TestComp from '../Components/TestComp'
import TestCompOne from '../Components/TestCompOne'
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
const drawerWidth = 240;
const useStyles = makeStyles((theme) => ({
root: {
display: 'flex',
},
drawer: {
[theme.breakpoints.up('sm')]: {
width: drawerWidth,
flexShrink: 0,
},
},
appBar: {
[theme.breakpoints.up('sm')]: {
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: drawerWidth,
},
},
menuButton: {
marginRight: theme.spacing(2),
[theme.breakpoints.up('sm')]: {
display: 'none',
},
},
// necessary for content to be below app bar
toolbar: theme.mixins.toolbar,
drawerPaper: {
width: drawerWidth,
},
content: {
flexGrow: 1,
padding: theme.spacing(3),
},
}));
function ResponsiveDrawer(props) {
const { container } = props;
const classes = useStyles();
const theme = useTheme();
const [mobileOpen, setMobileOpen] = React.useState(false);
const [sampleText, setSampleText] = React.useState("test");
const handleDrawerToggle = () => {
setMobileOpen(!mobileOpen);
};
const testCompClicked = () => {
setSampleText("Test Comp")
};
const testCompOneClicked = () => {
setSampleText("Test Comp One")
};
const drawer = (
<div>
<div className={classes.toolbar} />
<Divider />
<MenuList>
<MenuItem onClick={testCompClicked} component={Link} to="/testcomp">
<ListItemIcon>
<InboxIcon />
</ListItemIcon>
<ListItemText primary="Test Comp" />
</MenuItem>
<MenuItem onClick={testCompOneClicked} component={Link} to="/testcompone">
<ListItemIcon>
<MailIcon />
</ListItemIcon>
<ListItemText primary="Test Comp One" />
</MenuItem>
</MenuList>
<Divider />
</div>
);
return (
<div className={classes.root}>
<CssBaseline />
<AppBar position="fixed" className={classes.appBar} style={{ background: '#78a02e'}}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
edge="start"
onClick={handleDrawerToggle}
className={classes.menuButton}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap>
Responsive drawer
</Typography>
</Toolbar>
</AppBar>
<nav className={classes.drawer} aria-label="mailbox folders">
{/* The implementation can be swapped with js to avoid SEO duplication of links. */}
<Hidden smUp implementation="css">
<Drawer
container={container}
variant="temporary"
anchor={theme.direction === 'rtl' ? 'right' : 'left'}
open={mobileOpen}
onClose={handleDrawerToggle}
classes={{
paper: classes.drawerPaper,
}}
ModalProps={{
keepMounted: true, // Better open performance on mobile.
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden xsDown implementation="css">
<Drawer
classes={{
paper: classes.drawerPaper,
}}
variant="permanent"
open
>
{drawer}
</Drawer>
</Hidden>
</nav>
<main className={classes.content}>
<div className={classes.toolbar} />
<h1>{sampleText}</h1>
<h3>I want to show individual components using React Rounter when I press them from the left menu</h3>
<Router>
<Switch>
<Route path="/testcomp">
<TestComp />
</Route>
<Route path="/testcompone">
<TestCompOne />
</Route>
<Route path="/">
<TestComp />
</Route>
</Switch>
</Router>
</main>
</div>
);
}
ResponsiveDrawer.propTypes = {
/**
* Injected by the documentation to work in an iframe.
* You won't need it on your project.
*/
container: PropTypes.any,
};
export default ResponsiveDrawer;
Upvotes: 0
Views: 1913
Reputation: 1162
The Links
need to be within the <Router>
environment.
Try
return (
<Router>
<div className={classes.root}>
<CssBaseline />
<AppBar position="fixed" className={classes.appBar} style={{ background: '#78a02e'}}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
edge="start"
onClick={handleDrawerToggle}
className={classes.menuButton}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap>
Responsive drawer
</Typography>
</Toolbar>
</AppBar>
<nav className={classes.drawer} aria-label="mailbox folders">
{/* The implementation can be swapped with js to avoid SEO duplication of links. */}
<Hidden smUp implementation="css">
<Drawer
container={container}
variant="temporary"
anchor={theme.direction === 'rtl' ? 'right' : 'left'}
open={mobileOpen}
onClose={handleDrawerToggle}
classes={{
paper: classes.drawerPaper,
}}
ModalProps={{
keepMounted: true, // Better open performance on mobile.
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden xsDown implementation="css">
<Drawer
classes={{
paper: classes.drawerPaper,
}}
variant="permanent"
open
>
{drawer}
</Drawer>
</Hidden>
</nav>
<main className={classes.content}>
<div className={classes.toolbar} />
<h1>{sampleText}</h1>
<h3>I want to show individual components using React Rounter when I press them from the left menu</h3>
<Switch>
<Route path="/testcomp">
<TestComp />
</Route>
<Route path="/testcompone">
<TestCompOne />
</Route>
<Route path="/">
<TestComp />
</Route>
</Switch>
</main>
</div>
</Router>
);
Upvotes: 1