Next.js 13 Data Fetching - The Ultimate Guide

Sdílet
Vložit
  • čas přidán 2. 08. 2024
  • #nextjs #nextjs13 #fetchdata
    Next.js App router introduced a new, simplified data fetching system built on React and the web platform.
    In this video, I will take you through every concept when it comes to fetching data inside of the app router.
    We will go over fetching data on the server using server components, fetching dynamic data inside of dynamic route segments, using React Suspense to display loading states, data being deduplicated and much more.
    Private 1 on 1 Help 👇
    calendly.com/dabrettwestwood/...
    Join my FREE Discord to talk and network about web development! 👇
    / discord
    Data fetching an important topic when it comes to web development and Next.js 13 makes the development process smooth and easy to achieve.
    Next.js 13 Docs for data fetching 👇
    nextjs.org/docs/app/building-...
    GitHub Source Code 👇
    github.com/bwestwood11/data_f...
    Here is a link to the article I wrote for on fetching data in Next.js 13 App router 👇
    www.brettwestwood.dev/posts/N...
    Here is a link to the typicode.com 👇
    jsonplaceholder.typicode.com/
    Here is a link to the dog API 👇
    dog.ceo/dog-api/
    Time Stamps
    0:00 Intro
    0:21 Core Concepts for Data Fetching in Next.js 13
    1:59 Set up environment
    4:12 Fetching Data on Server from Dog API
    10:24 How To Always fetch fresh data
    11:48 Cleaning up our files/folder structure
    14:04 Server Side Data Fetching of ALL Users
    19:23 Fetching Dynamic Data
    26:18 Fetching Data in parallel
    33:01 Using React Suspense/Loading
    39:02 Outro
  • Jak na to + styl

Komentáře • 174

  • @GreatFootage8
    @GreatFootage8 Před 10 měsíci

    Thank you so much, I have been looking for new Nextjs tutorials, and this is the perfect one ! trust me, you are the only one that answered the questions in my head, all other suggestions by the docs/chatgpt/websites were either wrong, out of date or incomprehensible, keep going !

  • @caymangeiger
    @caymangeiger Před 8 měsíci +1

    I have searched everywhere for a guide like this and nobody has came close to this. Thank you! You are the only person who seems like they know what they are doing with app router on youtube

  • @eshw23
    @eshw23 Před rokem +4

    Just found your channel and subscribed, i like how your tutorials are long and in depth about concepts to understand instead of just project tutorials(those are great every now and then), and i dont think 10 or 15 minute short videos can do the same.

  • @traezeeofor
    @traezeeofor Před rokem +4

    This is exactly what i was looking for. A recent Next.js 13 Data Fetching tutorial. Thanks God, thanks Brett.

  • @urbanobaz
    @urbanobaz Před rokem +1

    Great video Brett! Always putting out quality content 🔥 thank you!

  • @mbaochachigozie1785
    @mbaochachigozie1785 Před rokem +1

    I appreciate you using the docs. Thanks for what you do

  • @noisinnang8371
    @noisinnang8371 Před 2 měsíci

    Thank you. I like how you explain each step slowly so a beginner like myself can follow and understand.

  • @pitamber_singh
    @pitamber_singh Před 8 měsíci +2

    Thank you so much Brett, for sharing such a wonderful tutorial, you did not even a miss a single thing in the tutorial. It may be a long tutorial for some guys but you explained each and everything for pro and beginners. Keep generating the tutorials. Subscribed you.

  • @gromhellscream5581
    @gromhellscream5581 Před 11 měsíci +1

    40 mins but worth it. Everything is explained really good. Thanks a lot for video!

  • @amadousjallow2080
    @amadousjallow2080 Před 11 měsíci +1

    I couldn't help it but I must subscribe, like and comment because the one thing I was looking for all these days you are the only one that explained it to my satisfaction thank you. I wish many people knew about this channel.

  • @OganicaBean
    @OganicaBean Před rokem +1

    Thank you Brett! Your channel helps me a lot for my job.

  • @btssugahit4663
    @btssugahit4663 Před 7 měsíci

    I am so thankful the video is amazing.....
    I watched a lot of videos and only this one helped so far Thanks buddy.

  • @abbasahmadvand6866
    @abbasahmadvand6866 Před rokem +2

    best tutorial😍

  • @AshutoshChoudhary2004
    @AshutoshChoudhary2004 Před 11 měsíci +1

    Very well explained!! This was exactly what I was looking for

  • @user-in1nu2fm1r
    @user-in1nu2fm1r Před 10 měsíci +1

    Simply the best!! Thumbs up Brett!!!

  • @misaelponce3082
    @misaelponce3082 Před rokem +1

    Nice video, have been looking for a tutorial like this, all topics were very clear, new sub

  • @elbezz
    @elbezz Před 11 měsíci +1

    Thanks Brett that was very well explained!

  • @YALW0506
    @YALW0506 Před 11 měsíci

    Thank you very much man, greetings from the Dominican Republic. - Subscribed!

  • @Yosie432
    @Yosie432 Před 11 měsíci +1

    Very well explained. Thank you! Great tutorials. Subscribed

  • @strangertongues
    @strangertongues Před 9 měsíci

    Great video! Thanks

  • @tundejoseph5733
    @tundejoseph5733 Před rokem +1

    Great tutorial, just what I was looking for. 😉

  • @miltondavilaharjula
    @miltondavilaharjula Před 9 měsíci +1

    Awesome video!! Thank you so much!!

  • @GarfieLD-Mami
    @GarfieLD-Mami Před 8 měsíci +2

    Today I discovered your channel, its amazing bro. Btw we are working on same board thx everthing.

  • @Winslow_Tech
    @Winslow_Tech Před 11 měsíci +1

    Great video that delivers on its promise (pun intended)
    Would have liked to see content around route handlers since this is still a confusing topic for someone coming from a non-nextjs react app.
    I would also love to see an advanced video covering the more complex problems such as launching the app with search/query params, how to securely authorize with third party endpoints using OAuth, keep sessions open/reconnect when expired and after establishing a secure connection, pull data into a component.
    One area of improvement I saw in the video is that some of the development bugs could have been caught earlier by installing ESLint and I'd recommend all beginners (and really anyone who wants to take an app to production) use ESLint and ideally TypeScript as well to make their lives easier when debugging.
    Thanks!

  • @ekchills6948
    @ekchills6948 Před rokem +1

    Very concise explanation. You're Awesome thanks

  • @JamesMiller-xs8zz
    @JamesMiller-xs8zz Před 11 měsíci +1

    Just found out this video and subcribed, thanks alot, keep up the good work

  • @flyflor599
    @flyflor599 Před 6 měsíci +1

    Thanks,very useful video!

  • @muhammadilhammaulidin4535

    The best tutor fetch api in next js🎉🔥🔥

  • @TITANSEsportsYT
    @TITANSEsportsYT Před rokem +1

    Was Helpful. Great Content.

  • @rockNbrain
    @rockNbrain Před rokem

    Great job bro ... tks 🎉

  • @NOTHING-en2ue
    @NOTHING-en2ue Před rokem +1

    very nice tutorial, thanks a lot ❤

  • @omarcodes5181
    @omarcodes5181 Před 11 měsíci

    Your amazing man !!

  • @btssugahit4663
    @btssugahit4663 Před 7 měsíci

    Thank you soo muchhhhhhh

  • @Gaulois_NRV
    @Gaulois_NRV Před rokem +1

    good teaching, keep it up !

  • @tarunsingh412
    @tarunsingh412 Před rokem +1

    Can i use next js loading page instead of suspense in my code?

  • @user-wf5vu6os9s
    @user-wf5vu6os9s Před rokem +1

    good introduction to fetch in next13

  • @Royale3533
    @Royale3533 Před 10 měsíci

    Hi, I have a question: I am trying to convert a regular React application over to Next.js. In my original application I use CancelTokens (axios) with useRef hook to clear out cancelled api calls, but of course using a useRef hook requires the component be rendered by the client ("use client"), which defeats the purpose of server side rendering. Do I just not need to cancel aborted calls when doing server side rendering? Or is there another way to do it that still allows me to use server side rendering?

  • @brgv_
    @brgv_ Před rokem

    Hey, Great tutorial.
    But do you know they an issue with the next js 13.4 that after the build, dynamic path [id] won't work. Static export is not supported.

  • @hjikingali7625
    @hjikingali7625 Před rokem +1

    thank you :)

  • @tanmaypatra4896
    @tanmaypatra4896 Před 9 měsíci

    Hello Brett. Instead of using dummy JSON API, can we use direct data fetching from Database and implement "revalidate" time? Please sugegst.

  • @sunbertyv3947
    @sunbertyv3947 Před 9 měsíci +1

    THANKS SO MUCH

  • @oussamajadidi6299
    @oussamajadidi6299 Před rokem +1

    thanks man,god bless you

  • @Mr.Thenula2011
    @Mr.Thenula2011 Před 8 měsíci

    can you show a fix for CORS while using localhost API , getting an fetch error from strapi backend

  • @luca4479
    @luca4479 Před rokem

    How would we fetch local data like from an ORM like prisma? Create a separate backend? Are you supposed to fetch your own api routes? Caching custom prisma functions never worked for me

  • @user-sv2zu4rt8s
    @user-sv2zu4rt8s Před 3 měsíci

    Can any tell me one thing he's fetching data with get api but I have post api how to implement and also have Authorization in header and usertoken save in redux we can not use hook in server component tell so I stuck pls tell me solution

  • @philipepics
    @philipepics Před 11 měsíci

    how about jest unit test async server components,? and how solve route handler fetch URL when run local npm run build, its require absolute path online, how solve when actually I don't have ?

  • @ali-ye7hx
    @ali-ye7hx Před 9 měsíci +1

    what extension are you using at 15:49 that gives you those code suggestions in the gray color on the editor?

  • @gggg-ij7zn
    @gggg-ij7zn Před 10 měsíci

    hi can someone help me please i want to send data from root page to child of another child can use redux state management?

  • @yousuf4you
    @yousuf4you Před rokem

    What will happen, if the endpoint return 404, 401 or any error?

  • @aryansonwani7061
    @aryansonwani7061 Před rokem +1

    Sir you saved my ass thank u very much .... Appreciate it 👍

  • @hindolroy5561
    @hindolroy5561 Před 9 měsíci +1

    Hi Bret can you please tell whether this fetch that stores only cache data will also cache data for our rest api as well where in we fetch data from database or this caches data only when we call api from external sources?

  • @kylemyers5802
    @kylemyers5802 Před 3 měsíci +1

    Hey Brett, what extension were you using for the code completion that was populating in 6:44?

  • @echezonachukwuekwebene8047
    @echezonachukwuekwebene8047 Před 8 měsíci +1

    Hello Brett, how can I have access to redux states as well as dispatch actions in a server component?

  • @ktappdev
    @ktappdev Před 11 měsíci +1

    Subbed

  • @dragossebastiantruta454
    @dragossebastiantruta454 Před 10 měsíci +1

    How can i refatch on demand the data ? Let's say I want a button that will trigger a refresh on the getDogs, how can I do that ?

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 10 měsíci +1

      You can do this multiple ways. You can use a window.location.reload() or other methods as well.

  • @juansebastiannunez4321
    @juansebastiannunez4321 Před rokem +1

    Thank you for this video, I have a question, how i do handle the dynamic route if write url /users/11 but there are only 10 users? How I show that the page not exist?

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před rokem +1

      you can create an error.jsx file or notFound file as well. I can make a video going over that, but nobody should be manually typing 11. The docs currently go over those 2 file conventions

  • @chrisr2063
    @chrisr2063 Před 9 měsíci +1

    fallback is not being displayed for me. the three second timeout happens but does not show the user name with "Loading..." before loading the data, it does nothing. I even cloned your code to test but got the same result. Thoughts?

  • @matarloum2894
    @matarloum2894 Před rokem

    Client side fetching ?

  • @learner8084
    @learner8084 Před 3 měsíci

    I noticed that the Suspense program did not have a "use client". Thought that all react import will require client...

  • @francorueta3332
    @francorueta3332 Před 9 měsíci +1

    Hi! You saved my life, i've been watching a lot of videos and posts on how to do data fetching and most of them weren't working. I have only a small question.
    Im using nextUI table to try and render some users, the issue is that for the table to work the component must be "use client" (client side) And due to this, i can't make the component asynchronous, so in this kind of situation, how can you make a fetch from client side on demand?

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 9 měsíci

      You can make a fetch in the api folder and then make a call to your backend from your client component. This will fix your problem. So, make a fetch request in a useEffect to your backend api route. /api/users and then create your server side code there.

  • @areebahmedkhan2201
    @areebahmedkhan2201 Před 10 měsíci +1

    Thank you soo much for explaining everything in detail. i Just have one question what if we want to filter the data in server side .I am using use Effect that make it obvious it become a client side but i want to do it in server side way so what should i do? should i make another Api request or what. please provide your feedback thankyou 💝

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 10 měsíci +1

      First off thank you for subscribing to my channel! And you can us the filter method in your server side code instead of the useEffect Hook. You would want to use the filter higher order function and then send the filtered data to client side through json data (NextResponse.json(filtereddata)

  • @nigmadude
    @nigmadude Před rokem +1

    thanks bro

  • @Travelwithsimmi
    @Travelwithsimmi Před 11 měsíci +1

    Hey, great tutorial.
    I have a question: how to handle input on change data fetching without routing or using link. Basically data should be visible on the same page without redirecting on the same page just update the url(single page app)

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 11 měsíci

      Should be able to use a router.refrsh(). This should be placed in a finally block or the try block. Hope this makes sense

  • @dantrrrrr
    @dantrrrrr Před rokem +1

    hi, i dont remember but nextjs13 have a thing like hover a link and it auto fetch before access the page, could you help me find out ? thank you. and sorry for my english

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před rokem +1

      Yes I have a video on it. It's my Link & Router video about Next.js 13. You will only be able to prefetch data and hover a link if application is in production not in development.

    • @dantrrrrr
      @dantrrrrr Před rokem +1

      @@brettwestwooddeveloper thank you sir

  • @ojal.dev.
    @ojal.dev. Před 9 měsíci +1

    Please create a tutorial video on deploying a Next.js app to a Digital Ocean Droplet. This would be very helpful.

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 9 měsíci +1

      Thanks for tuning in but I have never done this before.

    • @ojal.dev.
      @ojal.dev. Před 9 měsíci

      ​@@brettwestwooddeveloper If it's possible for you, please give it a try.

  • @bishalrana5247
    @bishalrana5247 Před 11 měsíci +1

    The fetch in the beginning is not cached. Returns new dog image on reload. Is this expected in next js latest version ? or am i missing something here ?

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 11 měsíci

      By default, Next.js automatically caches the returned values of fetch in the Data Cache on the server. You would have to specify you don't want the data cached

    • @bishalrana5247
      @bishalrana5247 Před 11 měsíci

      @@brettwestwooddeveloper Thanks. I think caching only works in production. How is it working in dev for you ?

  • @henryokeke1858
    @henryokeke1858 Před 9 měsíci +2

    During deployment on vercel, i got a typeError on user variable. Seems the user is null or undefined initially and can't have the map array method on it.
    Please why is this?

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 9 měsíci

      sounds like a TypeScript error, so you need to have a condition before the error like if (user) {your code}. Something like that.

    • @henryokeke1858
      @henryokeke1858 Před 9 měsíci

      @@brettwestwooddeveloper okay
      I'll try that now.

  • @antares-the-one
    @antares-the-one Před 8 měsíci +1

    i wrapped my in layout.js file with a context provider in which I pass fetched data from server (I use GET function with fetch). Down in the app I update that data on the server with another POST function (uses fetch). Why the layout is being triggered to rerender every time the POST function fetches data to the server?

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 8 měsíci

      Providers must be defined in client outside and then imported into layout file

    • @antares-the-one
      @antares-the-one Před 8 měsíci

      @@brettwestwooddeveloper thanks. My project was set up this way. The problem was due to my fetch function on the API route having been writing to file which was triggering file watcher to rebuild the entire project ☠

  • @SuperYoda7
    @SuperYoda7 Před rokem +1

    Great video! But I have one question: do we always get params as a default prop on every page ? Or do we have to specify and pass it in like in other react components?

  • @alextoma402
    @alextoma402 Před rokem +1

    how did he just create that url for fetching posts out of thin air? Would be nice if someone could explain this to me

  • @VasileMidrigan-zc5zr
    @VasileMidrigan-zc5zr Před 9 měsíci +1

    "For more content just like this" sounds so familiar 😂

  • @abukhalidrifat3994
    @abukhalidrifat3994 Před rokem +1

    what is the difference between fetching data in server side and client side?

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před rokem +1

      There is a lot of differences, fetching data server allows for a quicker response time, hides keys on the servers, better for cyber protection and much more. Here is a link to the Next.js 13 docs as well that lists the benefits of both: nextjs.org/docs/app/building-your-application/data-fetching

  • @hemeshthakur3865
    @hemeshthakur3865 Před rokem +1

    How do I fetch server side rendered data from a component that lives on the client side? Please make a video in it

  • @GabrielMartinez-ez9ue
    @GabrielMartinez-ez9ue Před rokem +1

    Hi Brett. Do you perhaps have a video on how to do updates without having to mess with rest api?

  • @angelicking2890
    @angelicking2890 Před rokem +1

    I am reading the docs and it says "Second, you can move data fetching lower in the component tree to only block rendering for the parts of the page that need it. For example, moving data fetching to a specific component rather than fetching it at the root layout." I am looking at the data-fetching docs for app router. Is that what you showed at 33:00? Great Video btw!

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před rokem

      Thank you! And I yes I think you are referring to the concept of fetch data where needed even if it has been already fetched.

  • @hakancavdar3661
    @hakancavdar3661 Před rokem +1

    Can some body help me ? I have to create an filter, and call an api with the filter element. So how can i call server side api call with client side state ?
    params defined by client : const [filter, setFilter]= useState([])
    call server api : const data = getProducts(filter)

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před rokem

      you must use the filter higher order function. That will just store the filter information into state

  • @aymanechaaba
    @aymanechaaba Před rokem +1

    What about error state?

  • @tanny3080
    @tanny3080 Před rokem +1

    Would it be better if we use ReactQuery for data fetching ?

  • @yousuf4you
    @yousuf4you Před rokem

    Please show us, how to handle error at the time of data fetching. like, 404, 500, 400

  • @ryanpratama14
    @ryanpratama14 Před rokem +1

    please do PUT, PATCH and POST also. And how can I revalidate data fetching after a POST request?

  • @sapi0ra
    @sapi0ra Před 10 měsíci +1

    Are you using Copilot or something similar?

  • @M.4y
    @M.4y Před 8 měsíci +1

    How to catch errors and do not prevent the whole site from loading?

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 8 měsíci

      Sorry confused on what you are trying to ask

    • @M.4y
      @M.4y Před 8 měsíci

      @@brettwestwooddeveloper Error catching

  • @codingwithcode3083
    @codingwithcode3083 Před 11 měsíci

    Please make video how to fetch Twitter trending data using nextjs

  • @HueCodes
    @HueCodes Před 6 měsíci

    Great vidoe man, and great explination i have trouble before even with nextjs docs, keep it up sub from me and notifaction on also.

  • @puruzsuz31
    @puruzsuz31 Před 6 měsíci

    WHERE IS REVALIDATE?????????

  • @Ja-rz9fq
    @Ja-rz9fq Před 4 měsíci +1

    And booom, just like that I threw away 15 minutes of my life. Thank God I skipped through the video and didn't watch all 40 minutes cause he didn't show the most crucial thing- how to combine async and hooks (server and client). At 11:25, instead of constantly reloading the page there should be some useState, and some button that we can click on and each time we click we get the new image. That's actually the real issue that we have with the new router not this bs...

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 4 měsíci

      I’m glad I contributed to wasting 15 minutes of your life. Maybe your whole life is a waste?🤔

    • @Ja-rz9fq
      @Ja-rz9fq Před 4 měsíci +2

      @@brettwestwooddeveloper thanks dude, I'm good-looking at least. You know, that's when you don't need to have funny haircuts or favorable lighting to not look like tiny $h1t. Cheers

    • @Ja-rz9fq
      @Ja-rz9fq Před 4 měsíci +2

      @@brettwestwooddeveloper Well that's a very gay thing to say dude... (dont delete replies I've read that)

  • @wasd3108
    @wasd3108 Před 11 měsíci

    terrible audio, I hear popping and crackling in my speakers, I am out

  • @emrahinklavyesi
    @emrahinklavyesi Před 10 měsíci +1

    Hi Brett, I watched your tutorial and it is perfect, thank you very much, but there is a small problem, the userPage page works but {user.name} does not work for me, you have a solution I can't see the detail page, I would be very grateful if you could help me. :)

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Před 10 měsíci

      Thanks for watching my video! And yes I can help you out. It can be a few issues. First I would console log the user.id to make sure you are getting a value. If you aren't getting a value then maybe the url for the fetch request is wrong. Let me know and keep me updated

  • @zerchaboi2223
    @zerchaboi2223 Před 4 měsíci +1

    One of the cleanest explaination i have ever heard in English 🤍its that good uk....