Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

javascript - 单击图标按钮reactjs后临时导航栏未关闭(Temporary Navigation bar not closing after clicking on a icon button reactjs)

I created a navigation bar from the material-ui website and I have a onClick that when the user clicks the icon button the navigation will get redirected to a new page and I would like for the navigation to close afterwards.(我从material-ui网站创建了一个导航栏,并且有一个onClick,当用户单击图标按钮时,导航将重定向到新页面,我希望此后关闭导航。)

I'v tried different things, but for some reason it will not close.(我尝试了不同的方法,但是由于某种原因,它不会关闭。) The only thing that it does now is gets redirected to a new page and the navigation drawer continues to stay open.(现在唯一要做的就是重定向到新页面,导航抽屉继续保持打开状态。) I have aa function called handleDrawerClose() that closes the drawer, a const called navigation that creates the text and components and I created a const called handleNavigation that pushes the links, which makes the page redirect.(我有一个称为handleDrawerClose()的函数来关闭抽屉,一个名为Navigation的const创建文本和组件,并且我创建了一个名为handleNavigation的const来推送链接,从而使页面重定向。) Is there a way to call both of these someway.(是否有某种方式可以同时调用这两种方法。) Thank you.(谢谢。) Below is my code:(下面是我的代码:) const navigation = [ { to: '/', text: 'Upload', Icon: InboxIcon }, { to: '/email', text: 'Send', Icon: MailIcon } ] const NavLinks = ({ links, onClick }) => { const _onClick = to => () => onClick(to); return ( <List> {links.map(({ to, text, Icon }) => ( <ListItem key={to} button onClick={_onClick(to)}> <ListItemIcon> <Icon /> </ListItemIcon> <ListItemText primary={text} /> </ListItem> ))} </List> ) } const IconArrow = ({ onClick }) => { return ( <IconButton onClick={onClick}> <ChevronLeftIcon /> </IconButton> ) } export default withRouter(({ history }) => { const classes = useStyles(); const [_, { logout }] = useAppAuth(); const [open, setOpen] = React.useState(false); const [state, setState] = React.useState({ left: false, }); function handleDrawerOpen() { setOpen(true); } function handleDrawerClose() { setOpen(false); } const handleNavigation = to => () => history.push(to); return ( <Fragment> <AppBar position='static'> <Toolbar className={classes.toolbar}> <IconButton color='inherit' edge='start' aria-label='open menu drawer' onClick={handleDrawerOpen} > <MenuIcon /> </IconButton> <Link to='/'> <img alt='Logo' src={logo} className={classes.image} /> </Link> <Typography variant='h6' className={classes.title}> {process.env.REACT_APP_NAME} </Typography> <Tooltip title='Logout'> <Link to='/login'> <IconButton onClick={logout} className={classes.logout}> <LogoutIcon /> </IconButton> </Link> </Tooltip> </Toolbar> </AppBar> <Drawer className={classes.drawer} variant='temporary' anchor='left' open={open} > <div className={classes.iconArrow}> <IconArrow onClick={handleDrawerClose} /> </div> <Divider /> <NavLinks links={navigation} onClick={() => handleNavigation} /> </Drawer> </Fragment> ) })   ask by alphanumEric translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why dont you just close it in the handler?(您为什么不只在处理程序中将其关闭?)

const handleNavigation = to => { setOpen(false); // add this history.push(to); }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...