hsz hsz - 3 months ago 13
React JSX Question

Redirect to another route when `mapHooks` fails

I have defined following

mapHooks
:

const mapHooks = {
fetch: ({ dispatch, params }) => dispatch(fetchPost(params.postId)).catch(() => {
console.log('catch');
}),
};


Sometimes, post cannot be found and I'd like to redirect user to the another page.
When I
catch
the error I tried to call
push('/foo')
to redirect to
/foo
page, but it does nothing (
push
comes from
react-router-redux
.

How can I redirect user properly ?

Answer

push function that comes with react-router-redux is an action creator, meaning that it returns an action that you need to dispatch. So you need to call it like this: dispatch(push('/foo'))

Also, check if you have your routerMiddleware installed correctly.