Skip to content
Advertisement

Next.JS Redux dispatch not working in getStaticProps()

I am pretty new to Next.JS and I was trying to set up Redux with my Next.JS application. Now my page is supposed to display a list of posts that I am calling in from an API. The page renders perfectly when I’m dispatching from useEffect() to populate the data on to my page, but getStaticProps() or getServerSideProps() are not working whatsoever!

Here is a bit of code that will give you a hint of what I’ve done so far:

store.js

JavaScript

action.js

JavaScript

_app.js

JavaScript

These are the files that I needed for the basic redux setup. Once my store was set up and I wrapped my app around the Provider, I initially though of using useEffect() hook to populate data on a component that was rendering inside my index.js file.

component.js

JavaScript

This worked perfectly! All my posts were showing up inside the component. The problem occurred when I was trying to achieve the same behaviour with server side rendering (or even SSG). I wanted to populate the data during the pre-render phase but for some reason the items array which is supposed to hold all the data is empty, basically meaning that the disptacher was never called! Here is the piece of code that is bothering me (exactly same as previous code, but this time I’m using getStaticProps() instead of useEffect()):

component.js

JavaScript

By running this, I’m getting an error that items is empty! Please help me, I have no clue what’s going wrong here.

Advertisement

Answer

Well I fixed this issue myself but I forgot to post an answer for it, my bad!

The problem here really is very simple, hooks don’t work outside of a functional component!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement