Redux-Persist无法与NextJs的SSR .getInitialProps()函数一起使用吗?

时间:2020-07-13 11:53:16

标签: redux state next.js server-side-rendering redux-persist

我尝试为我的SSR NextJs应用程序实现redux-persist,但没有成功。我几乎使用了基本用法示例https://github.com/rt2zz/redux-persist#basic-usage

中的相同代码

正如你们中的某些人可能知道NextJs的.getInitialProps()(https://nextjs.org/docs/api-reference/data-fetching/getInitialProps)函数会在服务器端获取数据。所以对我来说Redux-Persist仅在已经调用了persist / PERSIST和persist / REHYDRATE之后获取客户端数据时才起作用。 也许我做错了事或没有完全理解这个概念,但这是我的过程以及如何从reducer调用动作:

  1. {type: '@@redux/INITr.6.5.z.4.q'}
  2. {type: 'myDataFetchingReducer', payload: xxxxx }->通过getInitialProps()成功获取数据,使用新数据更新了redux状态和存储
  3. {type: '@@redux/INITr.6.5.z.4.q'}->状态和存储仍然很好
  4. {type: 'persist/PERSIST'}->状态和存储仍然很重要
  5. {type: 'persist/HYDRATE'}->状态和存储区被重置为状态, 在调用myDataFetchingReducer之前

在这一点上获得一些见解会很酷。我不知道我在这里做错了什么,还是我对它应该如何工作的理解在这里是错的。谢谢!

0 个答案:

没有答案