correct way to fetch data
Answered
codecret posted this in #help-forum
codecretOP
as beginner in nextjs what is the newest way to fetch data? is it using react-query
also in nextjs documentation why is he fetching data from data file ? shouldn't he do this using router handlers ?
also in nextjs documentation why is he fetching data from data file ? shouldn't he do this using router handlers ?
Answered by Sun bear
Thats one of the cool app router features. You can simply fetch them im your page.tsx. Then its fetched serverside. React query is great to fetch data client side.
But fetching serverside and using loading.tsx is my fav way
But fetching serverside and using loading.tsx is my fav way
4 Replies
Sun bear
Thats one of the cool app router features. You can simply fetch them im your page.tsx. Then its fetched serverside. React query is great to fetch data client side.
But fetching serverside and using loading.tsx is my fav way
But fetching serverside and using loading.tsx is my fav way
Answer
Netherland Dwarf
Adding to ^^
In that example, they seperate the fetching logic from the page, and in that fetch function they made it uses the server. This is the best approach because next doc says its an antipattern to do a fetch request to your next/api route because that means extra network and slower performance
In that example, they seperate the fetching logic from the page, and in that fetch function they made it uses the server. This is the best approach because next doc says its an antipattern to do a fetch request to your next/api route because that means extra network and slower performance
So thats why they instead just did a simple fetch request but they moved the logic to a seperate function just for good coding practice.
However there are 3 approaches i would say:
1) using next/api
2)using next route.ts
3) normal fetching (can use react query, axios etc anything)
1) using next/api
2)using next route.ts
3) normal fetching (can use react query, axios etc anything)