What can be the replacement for getInitialProps if data needs to be fetched globally in a next js application | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What can be the replacement for getInitialProps if data needs to be fetched globally in a next js application

I wamt to fetch the data at server side and only fetch once for whole app so that i can pass it to multiple components at fast pace.. how can i do it?

15th Feb 2023, 2:19 AM
Doggy
Doggy - avatar
5 Answers
+ 2
The recommended approach for fetching data globally in a Next.js application is to use the getStaticProps or getServerSideProps methods. Both of these methods allow you to fetch data from an external API or database and then render it on the page when it is requested. The main difference between these two methods is that getStaticProps will only be called once when the page is built, whereas getServerSideProps will be called every time the page is requested. This means that getStaticProps is better suited for global data that doesn't change often, while getServerSideProps is better suited for data that changes more often.
15th Feb 2023, 6:17 PM
Last
Last - avatar
+ 2
Awesome buddy.. one more question please.. i want to fetch the data required for whole app and use it among multiple pages.. from what i understood .. on navigation to a new page.. i think that data will be refetched if i use those props function.. and if i only use one one page then ..what if user navigates directly to some new page..then how to manage data.. i m confused at this ..plz if u can help from what i got to know... i can use a redux store and save data to it once and use multiple times..right but... how to do it among multiple pages and at which page / component should i save data to redux..thats the problem.i m stucked in buddy 😭
16th Feb 2023, 4:26 PM
Doggy
Doggy - avatar
+ 2
dummy Redux is a great way to store and manage data across multiple pages. You can store data in the redux store when you navigate to a new page, and it will be available throughout the app. You can also store data in the store when you are on a single page, or when you navigate to a new page. It is up to you to decide where to store the data in the store, depending on what data you need to access and when.
17th Feb 2023, 4:23 PM
Last
Last - avatar
+ 2
Ok buddy thanku so much for clearing my doubt.. let me try with redux🤗
17th Feb 2023, 4:26 PM
Doggy
Doggy - avatar
+ 2
No problem! Redux is a great way to simplify state management in your application. I am sure you will find it very helpful. Good luck!
17th Feb 2023, 4:27 PM
Last
Last - avatar