Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use React Router with Material UI Drawer to change content on click?

I've tried using Component={Link} to={path} but that just changes the URL. The content is displayed inside main and I've used Routes inside there like so:

<main>
  <Switch>
    <Route path={..} component={..}>
    .
    .
    .
  </Switch>
</main

But this doesn't work, the content doesn't change

Link to material UI example: https://codesandbox.io/s/v66pl

like image 838
Sanjeev Rajasekaran Avatar asked Oct 22 '19 23:10

Sanjeev Rajasekaran


People also ask

How do you route to another component in react on button click?

To redirect to another page on button click in React: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function, passing it the path - navigate('/about') . The navigate() function lets us navigate programmatically.

How do you integrate material UI tabs with react router?

In order to do that, I will first import the dependency for react-router-dom . Next, I will import BrowserRouter component from react-router-dom . Then, I will wrap the entire implementation for with AppBar under BrowserRouter . The BrowserRouter uses the HTML5 history API to keep the UI in sync with URL.

Which component should be used to switch a route on a click?

The <Switch /> component will only render the first route that matches/includes the path. Once it finds the first route that matches the path, it will not look for any other matches.


1 Answers

Added to your sample as you requested: BrowserRouter, Link.

https://codesandbox.io/s/material-demo-605w9

like image 166
Oleg Avatar answered Oct 19 '22 22:10

Oleg