Next.js 13 Data Fetching - The Ultimate Guide

Поділитися
Вставка
  • Опубліковано 1 жов 2024

КОМЕНТАРІ • 176

  • @henryokeke1858
    @henryokeke1858 11 місяців тому +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  11 місяців тому

      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 11 місяців тому

      @@brettwestwooddeveloper okay
      I'll try that now.

  • @traezeeofor
    @traezeeofor Рік тому +4

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

  • @abbasahmadvand6866
    @abbasahmadvand6866 Рік тому +2

    best tutorial😍

  • @GarfieLD-Mami
    @GarfieLD-Mami 10 місяців тому +2

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

  • @francorueta3332
    @francorueta3332 11 місяців тому +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  11 місяців тому

      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.

  • @hakancav3661
    @hakancav3661 Рік тому +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  Рік тому

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

  • @areebahmedkhan2201
    @areebahmedkhan2201 Рік тому +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  Рік тому +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)

  • @chrisr2063
    @chrisr2063 11 місяців тому +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?

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  10 місяців тому

      I would honestly have to see the code. I wouldn't be able to tell you.

  • @kylemyers5802
    @kylemyers5802 5 місяців тому +1

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

  • @emrahinklavyesi
    @emrahinklavyesi Рік тому +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  Рік тому

      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

  • @hindolroy5561
    @hindolroy5561 11 місяців тому +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?

  • @dragossebastiantruta454
    @dragossebastiantruta454 Рік тому +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  Рік тому +1

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

  • @echezonachukwuekwebene8047
    @echezonachukwuekwebene8047 10 місяців тому +1

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

  • @eshw23
    @eshw23 Рік тому +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.

  • @angelicking2890
    @angelicking2890 Рік тому +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  Рік тому

      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.

  • @pitamber_singh
    @pitamber_singh 10 місяців тому +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.

  • @ali-ye7hx
    @ali-ye7hx 11 місяців тому +1

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

  • @antares-the-one
    @antares-the-one 10 місяців тому +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  10 місяців тому

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

    • @antares-the-one
      @antares-the-one 10 місяців тому

      @@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 ☠

  • @abukhalidrifat3994
    @abukhalidrifat3994 Рік тому +1

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

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Рік тому +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

  • @ojal.dev.
    @ojal.dev. 11 місяців тому +1

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

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  11 місяців тому +1

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

    • @ojal.dev.
      @ojal.dev. 11 місяців тому

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

  • @Ja-rz9fq
    @Ja-rz9fq 6 місяців тому +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  6 місяців тому

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

    • @Ja-rz9fq
      @Ja-rz9fq 6 місяців тому +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 6 місяців тому +2

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

  • @bishalrana5247
    @bishalrana5247 Рік тому +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  Рік тому

      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 Рік тому

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

  • @tanmaypatra4896
    @tanmaypatra4896 11 місяців тому

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

  • @sapi0ra
    @sapi0ra Рік тому +1

    Are you using Copilot or something similar?

  • @philipepics
    @philipepics Рік тому

    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 ?

  • @alextoma402
    @alextoma402 Рік тому +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

  • @flyflor599
    @flyflor599 8 місяців тому +1

    Thanks,very useful video!

  • @Mr.Thenula2011
    @Mr.Thenula2011 10 місяців тому

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

  • @VasileMidrigan-zc5zr
    @VasileMidrigan-zc5zr 11 місяців тому +1

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

  • @learner8084
    @learner8084 5 місяців тому

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

  • @JoshuaTrotskiy
    @JoshuaTrotskiy 5 місяців тому

    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

  • @hemeshthakur3865
    @hemeshthakur3865 Рік тому +1

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

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  Рік тому +1

      If you have to fetch it that way, then just make an api backend and run a GET request to it

    • @hemeshthakur3865
      @hemeshthakur3865 Рік тому +1

      @@brettwestwooddeveloper thanks sir❤

  • @miltondavilaharjula
    @miltondavilaharjula 11 місяців тому +1

    Awesome video!! Thank you so much!!

  • @HueCodes
    @HueCodes 8 місяців тому

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

  • @codingwithcode3083
    @codingwithcode3083 Рік тому

    Please make video how to fetch Twitter trending data using nextjs

  • @tarunsingh412
    @tarunsingh412 Рік тому +1

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

  • @Winslow_Tech
    @Winslow_Tech Рік тому +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!

  • @Yosie432
    @Yosie432 Рік тому +1

    Very well explained. Thank you! Great tutorials. Subscribed

  • @Travelwithsimmi
    @Travelwithsimmi Рік тому +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  Рік тому

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

  • @amadousjallow2080
    @amadousjallow2080 Рік тому +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.

  • @M.4y
    @M.4y 10 місяців тому +1

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

    • @brettwestwooddeveloper
      @brettwestwooddeveloper  10 місяців тому

      Sorry confused on what you are trying to ask

    • @M.4y
      @M.4y 10 місяців тому

      @@brettwestwooddeveloper Error catching

  • @juansebastiannunez4321
    @juansebastiannunez4321 Рік тому +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  Рік тому +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

  • @ryanpratama14
    @ryanpratama14 Рік тому +1

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

  • @gggg-ij7zn
    @gggg-ij7zn Рік тому

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

  • @gromhellscream5581
    @gromhellscream5581 Рік тому +1

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

  • @TITANSEsportsYT
    @TITANSEsportsYT Рік тому +1

    Was Helpful. Great Content.

  • @AshutoshChoudhary2004
    @AshutoshChoudhary2004 Рік тому +1

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

  • @JamesMiller-xs8zz
    @JamesMiller-xs8zz Рік тому +1

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

  • @caymangeiger
    @caymangeiger 10 місяців тому +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

  • @piricuaco
    @piricuaco Рік тому +1

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

  • @elbezz
    @elbezz Рік тому +1

    Thanks Brett that was very well explained!

  • @Naizahg
    @Naizahg Місяць тому

    8:21
    May I know what VSCode Addons you use for that NextJs auto complete tag snippet?

  • @dantrrrrr
    @dantrrrrr Рік тому +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  Рік тому +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 Рік тому +1

      @@brettwestwooddeveloper thank you sir

  • @aryansonwani7061
    @aryansonwani7061 Рік тому +1

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

  • @SuperYoda7
    @SuperYoda7 Рік тому +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?

  • @Royale3533
    @Royale3533 Рік тому

    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?

  • @tanny3080
    @tanny3080 Рік тому +1

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

  • @aymanechaaba
    @aymanechaaba Рік тому +1

    What about error state?

  • @brgv_
    @brgv_ Рік тому

    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.

  • @ekchills6948
    @ekchills6948 Рік тому +1

    Very concise explanation. You're Awesome thanks

  • @luca4479
    @luca4479 Рік тому

    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

  • @OganicaBean
    @OganicaBean Рік тому +1

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

  • @noisinnang8371
    @noisinnang8371 4 місяці тому

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

  • @btssugahit4663
    @btssugahit4663 9 місяців тому

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

  • @wasd3108
    @wasd3108 Рік тому

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

  • @strangertongues
    @strangertongues 11 місяців тому

    Great video! Thanks

  • @mbaochachigozie1785
    @mbaochachigozie1785 Рік тому +1

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

  • @yousuf4you
    @yousuf4you Рік тому

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

  • @sunbertyv3947
    @sunbertyv3947 11 місяців тому +1

    THANKS SO MUCH

  • @tundejoseph5733
    @tundejoseph5733 Рік тому +1

    Great tutorial, just what I was looking for. 😉

  • @GabrielMartinez-ez9ue
    @GabrielMartinez-ez9ue Рік тому +1

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

  • @hjikingali7625
    @hjikingali7625 Рік тому +1

    thank you :)

  • @YALW0506
    @YALW0506 Рік тому

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

  • @urbanobaz
    @urbanobaz Рік тому +1

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

  • @yousuf4you
    @yousuf4you Рік тому

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

  • @GreatFootage8
    @GreatFootage8 Рік тому

    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 !

  • @matarloum2894
    @matarloum2894 Рік тому

    Client side fetching ?

  • @ktappdev
    @ktappdev Рік тому +1

    Subbed

  • @NOTHING-en2ue
    @NOTHING-en2ue Рік тому +1

    very nice tutorial, thanks a lot ❤

  • @btssugahit4663
    @btssugahit4663 9 місяців тому

    Thank you soo muchhhhhhh

  • @sikandertariq-h2u
    @sikandertariq-h2u Рік тому +1

    Simply the best!! Thumbs up Brett!!!

  • @oussamajadidi6299
    @oussamajadidi6299 Рік тому +1

    thanks man,god bless you

  • @mernapplications
    @mernapplications Рік тому +1

    good introduction to fetch in next13

  • @Gaulois_NRV
    @Gaulois_NRV Рік тому +1

    good teaching, keep it up !

  • @rockNbrain
    @rockNbrain Рік тому

    Great job bro ... tks 🎉

  • @nigmadude
    @nigmadude Рік тому +1

    thanks bro

  • @muhammadilhammaulidin4535
    @muhammadilhammaulidin4535 Рік тому

    The best tutor fetch api in next js🎉🔥🔥

  • @omarcodes5181
    @omarcodes5181 Рік тому

    Your amazing man !!

  • @puruzsuz31
    @puruzsuz31 8 місяців тому

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

  • @zerchaboi2223
    @zerchaboi2223 6 місяців тому +1

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