1

I am attempting to redirect inside a redux action when an error occurs. I've tried setting up react-router-redux but I'm getting the following error.

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)

This is how the router and redux is being setup.

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

I'm trying to use it like this

export function fetchStoreHierarchy() {

    return function (dispatch) {

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

2 Answers 2

3

From react-router-redux version 4, you have to import routerActions, as follows:

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

And call it's push function, as:

return function (dispatch) {
    // other code ....
    dispatch(routerActions.push('/login'));
}
Sign up to request clarification or add additional context in comments.

Comments

0

you are getting error because there's no push method defined..... try calling routeActions.push('/login') like this...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.