question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redux state not updated on client side when fetching data in getServerSideProps

See original GitHub issue

Describe 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

  1. dispatch an action getServerSideProps
export const getServerSideProps = wrapper.getServerSideProps(
  async ({ store }) => {
    store.dispatch(getPosts())
  }
)
  1. 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 })
}
  1. 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:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kirill-konshincommented, Jan 18, 2021

I told you to await the dispatch:

export const getServerSideProps = wrapper.getServerSideProps(
  async ({ store }) => {
    AWAIT store.dispatch(getPosts()) // <----------- HERE
  }
)

Otherwise Next.js does not know when action is finished.

0reactions
bsor-devcommented, Mar 25, 2022

+1

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found