react-router-redux无法推送新的URL

时间:2017-05-24 20:03:15

标签: javascript reactjs react-router react-redux react-router-redux

我正在尝试在发生错误时在redux操作中重定向。我已经尝试设置react-router-redux但是我收到了以下错误。

Uncaught TypeError: Cannot read property 'push' of undefined
    at middleware.js:29
    at index.js:14
    at index.js:100
    at dispatch (applyMiddleware.js:45)
    at reports-actions.js:88
    at index.js:11
    at Object.dispatch (index.js:100)
    at ReportTemplate.componentWillMount (ReportTemplate.js:54)
    at ReactCompositeComponent.js:348
    at measureLifeCyclePerf (ReactCompositeComponent.js:75)

这就是路由器和redux的设置方式。

const reducers = combineReducers({
    customer: transactionTrackerCustomerReducer,
    hierarchy: transactionTrackerHierarchyReducer,
    item: transactionTrackerItemReducer,
    tender: transactionTrackerTenderReducer,
    transaction: transactionTrackerTransactionReducer,
    transactionTracker: transactionTrackerReducer,
    session: userSessionReducer,
    reports: reportsReducer
});
const middleware = applyMiddleware(createLogger(), thunk, routerMiddleware(hashHistory));
const store = createStore(reducers, compose(middleware));
ReactDOM.render(
    <Provider store={store}>
        <Router history={hashHistory}>
            <MuiThemeProvider muiTheme={getBrandTheme()}>
                <Switch>
                    <Route path="/login" component={Login}/>
                    <Route path="/" render={props => (<App/>)}/>
                </Switch>
            </MuiThemeProvider>
        </Router>
    </Provider>
    , document.getElementById('root')
);

我正试图像这样使用它

export function fetchStoreHierarchy() {

    return function (dispatch) {

        // other code ....
        dispatch(push('/login'));
     }
}

2 个答案:

答案 0 :(得分:3)

react-router-redux版本4 ,您必须导入routerActions,如下所示:

import { routerActions } from 'react-router-redux'

并将其称为push函数,如:

return function (dispatch) {
    // other code ....
    dispatch(routerActions.push('/login'));
}

答案 1 :(得分:0)

您收到错误,因为没有定义推送方法.....尝试调用routeActions.push(&#39; / login&#39;)就像这样......

<link rel="search"
      type="application/opensearchdescription+xml"
      title="searchTitle"
      href="pluginURL">