Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI Drawer Selection How to route?

I am trying to use the Material UI React drawer but getting confused on how to implement the selection so that a change is made to a component. For example, I have a selection called Get Videos that should call up my component that does an AXIOS call to S3 to get the files in the buckets.

How do I make the component switch on my selection? Should I use a route or should I set the state of the component to "active"? Any help would be appreciated..

Some Code

handleClick(event) {
  ????
}

render() {
    return (
        <div>
            <ListItem button>
                <ListItemIcon>
                    <ScheduleIcon/>
                </ListItemIcon>
                <ListItemText primary="Scheduler" onClick={this.handleClick.bind(this)}/>
            </ListItem>
            <ListItem button onClick={this.handleClick()}>
                <ListItemIcon>
                    <ViewListIcon/>
                </ListItemIcon>
                <ListItemText primary="Watch Saved Recordings"/>
            </ListItem>
        </div>
    );
}
like image 309
Axwack Avatar asked Jun 11 '18 15:06

Axwack


2 Answers

Assuming that you know how to use react-router-dom. if don't use this link to find a good guide.

here is an example of using react routing with a sidebar:

import React, { Component } from 'react';
import { ListItemIcon, ListItemText, Divider, IconButton, MenuList, MenuItem, Drawer } from '@material-ui/core';
import { Link, withRouter } from 'react-router-dom';
import { withStyles } from '@material-ui/core/styles';
import PropTypes from 'prop-types';

import routes from '../routes/routes';

export class Sidebar extends Component {
  constructor(props) {
    super(props);

    this.activeRoute = this.activeRoute.bind(this);
  }

  activeRoute(routeName) {
    return this.props.location.pathname.indexOf(routeName) > -1 ? true : false;
  }

  render() {
    const { classes, theme } = this.props;
    return (
      <div>
        <Drawer
          variant="permanent"
        >
          <MenuList>
            {routes.map((prop, key) => {
              return (
                <Link to={prop.path} style={{ textDecoration: 'none' }} key={key}>
                  <MenuItem selected={this.activeRoute(prop.path)}>
                    <ListItemIcon>
                      <prop.icon />
                    </ListItemIcon>
                    <ListItemText primary={prop.sidebarName} />
                  </MenuItem>
                </Link>
              );
            })}
          </MenuList>
        </Drawer>
      </div>
    );
  }
}

export default withRouter(Sidebar);

and your router file should look like this:

import { Home, ContentPaste, Notifications, AccountCircle } from '@material-ui/icons';
import HomePage from '../pages/Home/HomePage';
import ProfilePage from '../pages/Profile/ProfilePage';

const Routes = [
  {
    path: '/dashboard/home',
    sidebarName: 'Home',
    navbarName: 'Home',
    icon: Home,
    component: HomePage
  },
  {
    path: '/dashboard/profile',
    sidebarName: 'Profile',
    navbarName: 'Profile',
    icon: AccountCircle,
    component: ProfilePage
  }
];

export default Routes;

with activeRoute you can highlight the current route

hope this will help you

like image 115
Nadun Avatar answered Nov 10 '22 13:11

Nadun


Also, You can use hooks using useLocation

 import { useLocation } from "react-router-dom";

and then you can create the same behavior

      const [path, setPath] = useState("");

      let location = useLocation();

      useEffect(() => {
        setPath(location.pathname);
        console.log(location.pathname)
      }, [location,setPath]);

  const activetRoute = route => {
      console.log(route , path);
    return route === path;
  }

and

<List>
        {routers.map((item, key) => (
          <ListItem
            key={key}
            button
            component={Link}
            to={item.path}
            selected={activetRoute(item.path)}
          >
            <ListItemIcon>
              <item.icon />
            </ListItemIcon>
            <ListItemText primary={item.sidebarName} />
          </ListItem>
        ))}
      </List>
like image 30
Alfredo Izquierdo Avatar answered Nov 10 '22 13:11

Alfredo Izquierdo