从动作重定向到react-redux-router中的页面

时间:2017-06-21 09:14:10

标签: reactjs redux react-redux

我有一个从服务器获取数据的常见操作。我从各种组件调用该动作。我想重定向,如果响应代码是x

export  function commonApiCall(callUrl,data,reducer,method)
  {

    return dispatch =>
    {

      .....
      ....
      axios({
      method: callMethod,
      url: ,
      data: '',
      headers: { 
        'Accept': 'application/json',
      },
    }).then( (response) => {
        if ( response.code == 'x'){
          // redirect I want to redirect here.
        }
        dispatch({
          type: reducer,
        });
      })
      .catch( (error) => {
        console.warn('Actions - fetchJobs - recreived error: ', error)
      })
    }
  }

一种解决方案是在每个组件中添加检查。但是,我想要一个集中式解决方案。

2 个答案:

答案 0 :(得分:0)

您可能需要import {push} from 'react-router-redux'并根据响应代码检查调度推送事件。

答案 1 :(得分:0)

您可以使用browserhistory.push('/sub-url')将页面路线推送到您的命运路线