【问题标题】:Material UI Drawer Selection How to route?Material UI Drawer Selection 如何路由?
【发布时间】:2018-11-20 21:39:42
【问题描述】:

我正在尝试使用 Material UI React 抽屉,但对如何实现选择以便对组件进行更改感到困惑。例如,我有一个名为 Get Videos 的选项,它应该调用我的组件,该组件对 S3 执行 AXIOS 调用以获取存储桶中的文件。

如何在我的选择中切换组件?我应该使用路由还是应该将组件的状态设置为“活动”?任何帮助将不胜感激..

一些代码

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>
    );
}

【问题讨论】:

  • 您能否提供更多有关情况的信息?您想在单击 ListItem 时转到另一个页面吗?
  • 是的,我想路由到另一个页面。

标签: reactjs material-ui


【解决方案1】:

假设你知道如何使用 react-router-dom。如果不使用this 链接找到一个好的指南。

这里是使用带有侧边栏的 react 路由的示例:

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);

您的路由器文件应如下所示:

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;

使用 activeRoute 可以突出显示当前路线

希望对你有帮助

【讨论】:

    【解决方案2】:

    另外,你可以使用 useLocation 来使用钩子

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

    然后你可以创建相同的行为

          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;
      }
    

    和

    <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>
    

    【讨论】:

    • 是否有一个如何实现的示例?我正在尝试使用 Material UI 抽屉来解决这个问题 - 但不确定如何定义路线
    • 为什么你的useEffect第二个参数需要setPath?
    猜你喜欢
    • 1970-01-01
    • 2018-04-04
    • 2021-06-30
    • 2018-07-05
    • 2022-01-15
    • 2019-01-10
    • 2017-08-04
    • 2021-06-28
    • 2017-09-20
    相关资源
    最近更新 更多