使用Redux,NextJS和SSR从API获取数据

时间:2020-07-18 02:14:16

标签: javascript reactjs redux next.js

我正在使用next-redux-wrapper,我可以将数据存储到服务器存储中,但是无法将其发送到客户端存储中。 我的代码如下:

在/ pages / index中:

export const getServerSideProps = wrapper.getServerSideProps(async (ctx) => {
  ctx.store.dispatch(testFunc(ctx));
  return {props: {data: ctx.store.getState().test.data}}
})

在商店/../动作中:

export const testFunc = (ctx) => (dispatch) => {
  return fetch(url)
    .then(r => r.json())
    .then(r => {
      const data = r;
      dispatch({type: actionTypes.DATA, data});
    })
}

我该怎么办?

0 个答案:

没有答案