Redux state not updated on client side when fetching data in getServerSideProps
See original GitHub issueDescribe the bug
Redux state is not updated when you dispatch an action in getServerSideProps
.
The redux state in server side looks fine but when the server tries to render the UI, the state does not look right. The client does not have the updated state
To Reproduce
- dispatch an action
getServerSideProps
export const getServerSideProps = wrapper.getServerSideProps(
async ({ store }) => {
store.dispatch(getPosts())
}
)
- The action creator calls a rest end point for data.
export const getPosts = () => async (dispatch) => {
const { data } = await Axios.get('https://jsonplaceholder.typicode.com/posts')
dispatch({ type: actionTypes.POSTS, payload: data })
}
- Th UI should update
const index = () => {
const dispatch = useDispatch()
const { counter, posts } = useSelector((state) => state.data)
return (
<div>
<h1>
Posts: <span>{posts.length}</span>
</h1>
</div>
)
}
I’ve created this specific scenario and uploaded it to github https://github.com/JunayedMizan/next-js-with-redux
Expected behavior
I would expect the redux store in client side to update.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Redux store data being reset upon "getServerSideProps ...
Overview: I have a TypeScript NextJS project that I am using Redux-toolkit with to manage state. My file structure for my Redux store...
Read more >Data Fetching: getServerSideProps - Next.js
If your page contains frequently updating data, and you don't need to pre-render the data, you can fetch the data on the client...
Read more >Server Rendering - Redux - JS.ORG
To send the data down to the client, we need to: create a fresh, new Redux store instance on every request;; optionally dispatch...
Read more >Refreshing Server-Side Props - Next.js - Josh W Comeau
js to re-fetch the data, on demand, without doing a hard refresh of the whole page? In this short tutorial, I'll share the...
Read more >getserversideprops not being called - You.com - You.com
And if i directly redirect to the /pageb it shows the data. what is the problem? Open side panel. Redux store data being ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I told you to
await
thedispatch
:Otherwise Next.js does not know when action is finished.
+1