next-redux-wrapper: Redux store not updating with SSR?
I’ve got a component where the store updates fine on the client side with the following code:
componentDidMount() {
const { loadQuoteIfNeededConnect, id } = this.props
loadQuoteIfNeededConnect(id)
}
If I comment that out and attempt to use the code below, the store does not update:
static async getInitialProps({ store, query: { id } }) {
// this fetches the data, but store is not updated
await store.dispatch(loadQuoteIfNeeded(id))
return { id }
}
I’ve got a console.log directly above the return statement in my reducer, and I can see in my terminal that the data is in fact being fetched and returned properly with the code from getInitialProps(), but the store is not updating.
Any ideas?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 35 (15 by maintainers)
In my case it turns out I wasn’t returning the promise in my chain of dispatch functions. Working perfectly now!
For anyone else running into the same problem, here’s an example:
Component — using async/await
Actions — note that
loadQuoteIfNeeded()has an implicit return andgetQuote()is being returnedI have same issue, im using nextjs + redux toolkit I got the updated store on server, but i didnt get the store on client up to date like on server
Action
Store
@brunoqs Looks like you’re overwriting your preloaded state with default state:
You need to change the order.
https://github.com/vercel/next.js/tree/canary/examples/with-redux-wrapper follow this example.
https://github.com/vercel/next.js/blob/canary/examples/with-redux-wrapper/store/store.js
This configuration saves me.
@jamal-rahimzadegan https://github.com/kirill-konshin/next-redux-wrapper/blob/types-enchancements/packages/demo-saga/src/components/saga.tsx
@jamal-rahimzadegan create an action and use something like
redux-thunkorredux-promise-middlewareto dispatch it.@jamal-rahimzadegan
without
awaityou will create a detached promise chain which will be executed in parallel and won’t affect the resulting state that will be transferred to client beforepostRequestwill finish. Also as a side note, you can’t use store anywhere outside Next.js lifecycle hooks and/or components.@MSefer just follow the example: https://github.com/kirill-konshin/next-redux-wrapper#usage
@kirill-konshin Thank you for response. I’m using version 6. I handled the Hydrate action. This solved my problem.
I can’t help with MobX. Regarding redux you MUST explicitly tell NextJS to
awaituntil all actions are dispatched: https://github.com/kirill-konshin/next-redux-wrapper#async-actions-in-getinitialpropsPay close attention that you won’t have any unhandled promises that were created inside the action.