首页 > 解决方案 > _app (nextjs) 中的调度操作

问题描述

它在 _app.js 中,我们必须将我们的商店和 saga 与“withredux”和“withReduxSaga”连接起来,但是,是否也可以在 _app.js 中调度操作?像那样 :

async getInitialProps({ Component, ctx }) {
      ctx.store.dispatch(fetchDataRestaurant(1));
    }

就我而言,它不起作用,它仅在带有“this.props.store.dispatch()”的组件中起作用

仅当我更改页面时,我的动作才最终在“getInitialProps”中调度......当应用程序加载时,动作没有调度,有人可以帮我吗?

谢谢你

标签: reactjsreact-reduxnext.jsredux-saga

解决方案


你可以试试redux-axios-middleware。它可能会解决问题。

static async getInitialProps ({ store }) {
  await store.dispatch(fetchDataRestaurant())
  return { restaurant: getRestaurant(store.getState()) }
}

推荐阅读