2017-06-21 73 views
0

我有一个从服务器获取数据的通用操作。我正在调用各种组件的动作。我想重定向,如果响应代码为x从动作重定向到react-redux-router中的页面

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) 
     }) 
    } 
    } 

一种解决办法是在每一个component.But,我希望有一个集中式解决方案添加检查。

+1

我想'push'是你在找什么,对不对? https://github.com/reactjs/react-router-redux#pushlocation-replacelocation-gonumber-goback-goforward – Khang

回答

0

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

+0

同样,我需要历史。我将如何在行动中获得这一点。 – learner

+0

根本不工作。 – learner

+0

请参阅https://github.com/reactjs/react-router-redux –

0

您可以使用browserhistory.push('/sub-url')其推动网页路线,你的命运路线

+0

我正在使用react-router v4,其中browserhistory不可用。 – learner

+0

https://github.com/reactjs/react-router-redux 查看此链接并找到教程部分,在那里你也应该使用反应路由器来解决你的问题 –

+0

你在你的应用中使用react Router吗?如果不是这样,请安装必不可少的反应路由器 –