gpt4 book ai didi

javascript - redux 如何使用 react-router 在 componentWillMount 上重定向

转载 作者:行者123 更新时间:2023-12-03 20:03:46 24 4
gpt4 key购买 nike

如何使用 react-router 在 componentWillMount 上重定向

export class NewStock extends React.Component {
constructor(props, context) {
super(props, context);
}

componentWillMount() {
const { session, dispatch, router } = this.props
if (session.userSessionData.get('logged_in') == false) {
router.transitionTo('/login')
}
};

这段代码:

router.transitionTo('/login')

只返回:

Uncaught TypeError: router.transitionTo is not a function

最佳答案

尝试this.props.history:

const { history } = this.props
history.pushState(null, '/login') or
history.replaceState(null, '/login')

如果你的模块不是路由器的直接后代,你仍然可以通过将你的组件包装在 withRouter(YourComponent) HOC(在 v4 中)来访问这个 prop。

关于javascript - redux 如何使用 react-router 在 componentWillMount 上重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34284787/

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