【问题标题】:What is the best way to redirect to another page after successful action in Redux?在 Redux 中成功操作后重定向到另一个页面的最佳方法是什么?
【发布时间】:2020-11-13 13:33:01
【问题描述】:

我正在尝试通过发送“AUTH_SUCCESS”操作成功登录并给我他们的令牌来重定向用户。有几种方法可以做到这一点,但我需要你的建议。哪一种是之后重定向他们的最佳方法登录?无论是在操作中还是在功能组件中使用某些东西。注意:我正在使用功能组件。谢谢!

【问题讨论】:

标签: reactjs redux redux-thunk


【解决方案1】:

伪代码

对于 thunk,只需在您的登录事件中传递 this.props.history

前端某处..

login = (e) => {
 e.preventDefault();
 const creds = { username: username, password: password}
 this.props.login(creds, this.props.history);
}

重击

export const login = (credentials, history) => {
  return (dispatch) => {
     .....
    /// after successful
    history.push('/dashboard);
   }
}

【讨论】:

    猜你喜欢
    • 2011-10-31
    • 2010-09-17
    • 2011-11-02
    • 2020-06-16
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多