如何使用Redux Thunk从Action Creators更改路由器?

时间:2018-02-05 11:46:11

标签: reactjs redux react-router redux-thunk

我决定将所有逻辑从React Components转移到Action Creators中。为此,我将经典的Redux动作创建器迁移到异步Redux-Thunk动作创建器。

基本流程非常出色。但我不知道如何从异步Action Creators更新路由器:(

import axios from 'axios';
import * as dialogActions from './DialogActions';

export const login = (creds) => {
  return (dispatch) => {
    axios.post('/admin/login', creds)
      .then((res) => {
        localStorage.setItem("token", res.headers['authorization']);

        //Next line gives error
        this.props.history.push("/admin/home");

        dispatch(dialogActions.initErrorAction(false, ''));
      })
      .catch((err) => {
        console.log(err.message)
        dispatch(dialogActions.initErrorAction(true, 'Invalid email or password'));
      });
  }
}

所以问题是:如何从Redux-Thunk Action Creators更新路由器?

0 个答案:

没有答案