`mapHooks` 失败时重定向到另一条路由

Redirect to another route when `mapHooks` fails

我定义了以下 mapHooks:

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

有时找不到 post,我想将用户重定向到另一个页面。 当我 catch 错误时,我试图调用 push('/foo') 重定向到 /foo 页面,但它什么也没做(push 来自 react-router-redux.

如何正确重定向用户?

push react-router-redux 附带的函数是一个 动作创建器 ,这意味着它 returns 您需要 dispatch。所以你需要这样称呼它:dispatch(push('/foo'))

此外,请检查您的 routerMiddleware 是否安装正确。