1

我試圖在發生錯誤時重新引導redux操作。我試過設置react-router-redux,但是我收到以下錯誤。react-router-redux無法推送新的URL

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) 

這是如何設置路由器和REDX。

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

回答

1

react-router-redux版本4,你必須輸入routerActions如下:

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

,並調用它的push功能,如:

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

你得到錯誤,因爲有沒有定義推送方法.....嘗試調用routeActions.push(「/登錄」)像這...

import { routeActions } from 'react-router-redux'; 
dispatch(routeActions.push('/login'));