【发布时间】:2018-05-01 11:18:05
【问题描述】:
我有一个这样的 NavLink 组件:
<NavLink isActive={(match, location) => this.isActiveFunc(match, location)} className={classes.subLink} to={{ pathname: "/admin/users" }}>
<ListItem button className={classes.nested}>
<ListItemText disableTypography={this.state.activePath === "/admin/users"} inset primary="Users" />
</ListItem>
</NavLink>
并尝试在每次 NavLink 处于活动状态时设置我的状态,如下所示:
updateActivePath = (match) => {
if (match && this.state.activePath !== match.path) {
this.setState({activePath: match.path})
}
};
isActiveFunc = (match, location) => {
this.updateActivePath(match);
};
然而,这给了我以下错误:
警告:在现有状态转换期间无法更新(例如 在
render或其他组件的构造函数中)。渲染方法 应该是 props 和 state 的纯函数;构造函数副作用 是反模式,但可以移动到componentWillMount。
有什么办法可以解决这个问题吗?
【问题讨论】:
-
是 react-router 的 NavLink 吗?
-
来自 react-router-dom
-
为什么要将活动路径复制到本地状态?只需从 react-router-dom 中获取活动路径
-
这似乎不起作用,因为我的组件不在
中(它是始终显示的导航栏)
标签: reactjs react-router