【发布时间】:2019-10-07 12:12:27
【问题描述】:
我有这个 Material-UI AppBar:
import React from 'react'
import AppBar from 'material-ui/AppBar'
import AccountCircle from 'material-ui-icons/AccountCircle'
import Toolbar from 'material-ui/Toolbar'
import IconButton from 'material-ui/IconButton'
import HomeIcon from 'material-ui-icons/Home'
import Button from 'material-ui/Button'
import auth from './../auth/auth-helper'
import {Link, withRouter} from 'react-router-dom'
const isActive = (history, path) => {
if (history.location.pathname == path)
return {color: '#c61054'}
else
return {color: '#ffffff'}
}
const Menu = withRouter(({history}) => (
<AppBar position="static">
<Toolbar>
<Link to="/">
<IconButton aria-label="Home" style={isActive(history, "/")}>
<HomeIcon/>
</IconButton>
</Link>
{
auth.isAuthenticated() && (<span style={{ marginLeft: "auto" }}>
<Link to="/issues">
<Button style={isActive(history, "/issues")}>Issues
</Button>
</Link>
<Link to="/users">
<Button style={isActive(history, "/users")}>Users
</Button>
</Link>
<Link to="/signup">
<Button style={isActive(history, "/signup")}>Create User
</Button>
</Link>
</span>)
}
{
!auth.isAuthenticated() && (<span style={{ marginLeft: "auto", marginRight: -12 }}>
<Link to="/signin">
<Button style={isActive(history, "/signin")}>Sign In
</Button>
</Link>
</span>)
}
{
auth.isAuthenticated() && (<span style={{ marginLeft: "auto", marginRight: -12 }}>
<Link to={"/user/" + auth.isAuthenticated().user._id}>
<IconButton aria-label="MyProfile" style={isActive(history, "/user/" + auth.isAuthenticated().user._id)}>
<AccountCircle/>
</IconButton>
</Link>
<Button color="inherit" onClick={() => {
auth.signout(() => history.push('/'))
}}>Sign Out</Button>
</span>)
}
</Toolbar>
</AppBar>
))
export default Menu
注销后,一切都会按预期显示。 Home 图标位于左侧,SIGNIN 位于右侧:
然后我再次登录,一切都如我所愿。 Home 图标、ISSUES、USERS 和 CREATE USER 位于左侧,Profile 图标和 SIGNOUT 位于右侧:
如果我再刷新页面,ISSUES、USERS和CREATE USER跳转到页面中心:
如何阻止这种情况发生?我应该尝试向 isActive 添加一些条件来设置它吗?也许我可以在函数调用中传递left 或right,并根据它的样式在按钮上设置。对此有什么想法吗?
【问题讨论】:
标签: javascript user-interface material-ui appbar