gpt4 book ai didi

reactjs - Material UI抽屉选择如何走线?

转载 作者:行者123 更新时间:2023-12-03 13:40:04 26 4
gpt4 key购买 nike

我正在尝试使用 Material UI React 抽屉,但对如何实现选择以便对组件进行更改感到困惑。例如,我有一个名为“获取视频”的选择,它应该调用我的组件,该组件对 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>
);
}

最佳答案

假设您知道如何使用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,您可以突出显示当前路线

希望这对你有帮助

关于reactjs - Material UI抽屉选择如何走线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50801093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com