Please need Help!! How to correctly use getInitialProps and useEffect simultaneously? #15126
Replies: 2 comments
-
@illuminist Can you please help me out with this? |
Beta Was this translation helpful? Give feedback.
-
You can add another state for redux to indicate the initial props readiness. I will call it When fetching data in Then, connect that state to the component, connect You may need to distinguish the different between initial |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a listing page of Products that I intend to server render. So using NextJs's getInitialProps I fetch the list of Products and pass it on to the components and it works. There is a requirement, whenever a city changes(from Dropdown) in client-side, I need to refetch the updated list from the server, So I have this API call in a useEffect. This is somewhat the idea.
The problem is at step 3 below,
How do I Prevent this redundant call, the first time when the page is rendered on the client?
Well, just an if check inside useEffect whether data is present before making the api call doesn't work, because despite Products data being present in Redux store, if the city changes, the api call has to be made.
Beta Was this translation helpful? Give feedback.
All reactions