Fetching Data in React - Complete Tutorial

Поделиться
HTML-код
  • Опубликовано: 14 июн 2023
  • Join The Discord! → discord.cosdensolutions.io
    Source Code → github.com/cosdensolutions/co...
    In this video we talk about data fetching in React. This is a complete tutorial teaching you everything there is to know about how to fetch data in React. You'll learn about loading, handling error states, and even how to prevent race conditions using abort controllers. We'll use some hooks such as useState, useEffect, and useRef. We will use the fetch API from Node.js and we briefly mention React Query and how it can simplify things. Enjoy!

Комментарии • 118

  • @cosdensolutions
    @cosdensolutions  3 месяца назад +2

    Hey everyone! I just launched 🚀 Project React, which is a course that teaches you React by building a real-world project. It goes way beyond what you see in these videos and walks you through step-by-step on how to build a big and complex application with React! You can check it out here: cosden.solutions/project-react

  • @aboaya2012
    @aboaya2012 6 месяцев назад +9

    I like that you state the basics of fetching data instead of only (how to) , so thanks a lot for your help

  • @craig1253
    @craig1253 4 месяца назад +10

    Been stuck learning react for months and now I found this gem of a channel. Keep it up bro!

  • @moviegift2492
    @moviegift2492 7 дней назад

    I like your way of teaching and I am new for react, i came from backend zone so I am here for the next coding sessions to see how really manages my love of data.
    Thank you for your effort. u got one sub.

  • @ooorayooonyoooo
    @ooorayooonyoooo 11 месяцев назад +2

    bro you are always just straight to the point. Thanks Man!

  • @praulayar4805
    @praulayar4805 5 месяцев назад

    one of the best video on data fetching with vanilla data fetching

  • @webdev-tom
    @webdev-tom 10 месяцев назад +9

    Your videos are such a masterpiece 👍 I really hope your channel will grow very fast because you deserve it so much. Really good quality content 💯

  • @user-xj5oq7kf7x
    @user-xj5oq7kf7x 7 месяцев назад +2

    This video was extremely informative! Looking forward to watching more React content from your channel. Would be great if you could also make a best practices video for production code or even intermediate/advanced React videos

  • @madhavvankina5201
    @madhavvankina5201 8 месяцев назад +2

    Your channel is gonna pop off. Your content is top-notch 💯💯

  • @KonstantinosKaminaris-xe8wt
    @KonstantinosKaminaris-xe8wt Месяц назад

    I aspire to one day be able to teach people like you do. This is really high quality teaching my friend. Respect.

  • @user-ux9iu3xo7n
    @user-ux9iu3xo7n 3 месяца назад +7

    I think u should also add some check statement for response.ok, because this is common case in handling responses from your own API, and u can receive response with status 400(some form validation error) or 500(some server internal error) and that's why its good practice to throw Error when !response.ok, btw, thanks for the video

  • @rishabhnegi9429
    @rishabhnegi9429 5 месяцев назад

    I am just obsessed with your videoes ... great work 😀

  • @jellyfish1772
    @jellyfish1772 6 месяцев назад

    Just when i saw you were using typescript I knew this would be a great tutorial. great video❤

  • @reihane2325
    @reihane2325 5 месяцев назад

    Thanks a lot for explain very clear. I saw lots of video in RUclips, but your explain is so helpful for me as a junior react typescript developer. Bon courage!

  • @tigranharutyunyan7674
    @tigranharutyunyan7674 6 месяцев назад

    Enthusiasm and knowledge! Thank you man

  • @lisalee7946
    @lisalee7946 3 месяца назад

    Best instructor!! Thank you for such great content!!💯

  • @iCeTainment
    @iCeTainment 9 месяцев назад +2

    I just subscribed 🎉
    Love your videos man! It’s helping me with my internship projects

  • @tylerxx26
    @tylerxx26 3 месяца назад

    Super stuff man. Please keep it up!🎉

  • @vesa95
    @vesa95 Год назад

    Fire man! 🔥 I’ve wait for you to post!

    • @cosdensolutions
      @cosdensolutions  Год назад

      Yes! More videos to come! I'm currently on vacation so I've posted a bit less

  • @jlogo80
    @jlogo80 Год назад +1

    Awesome stuff, thanks!

  • @erikreyes6093
    @erikreyes6093 9 месяцев назад +1

    wow very informative tutorial, there is so much to learn from you.
    I just hope you will keep creating vidoes that share your expertise in web development

  • @incarnateTheGreat
    @incarnateTheGreat 2 месяца назад

    Thanks for the Abort Controller solution!

  • @shahnawazk
    @shahnawazk 4 месяца назад +1

    One video to like and subscribe! Really helpful and informative. Thanks and keep it up.

  • @wallethe1614
    @wallethe1614 Год назад +1

    Awesome vid im learning alot, thank you and keep it going.

  • @ramirorenteria7333
    @ramirorenteria7333 Месяц назад

    Awesome tutorial!

  • @TLTechbender
    @TLTechbender 11 дней назад

    Thanks so much, you just helped me out big time!

  • @farhaannishtar786
    @farhaannishtar786 3 месяца назад

    This guy is good. Thanks for making great content

  • @MaxMov-sp8hr
    @MaxMov-sp8hr 4 месяца назад

    One of the best! 👏

  • @hamzapaskingakhtar
    @hamzapaskingakhtar 6 месяцев назад

    This is pure gold

  • @goodness776
    @goodness776 7 месяцев назад

    Never seen a lean step by step explanation. You are indeed a masterpiece @webdev-tom💯

  • @helenasdiaries515
    @helenasdiaries515 5 месяцев назад

    Thanks ! You are a great explainer

  • @INetreba
    @INetreba 4 месяца назад

    very good explanation!

  • @2difficult2do
    @2difficult2do 9 месяцев назад +1

    This is very interesting video, exactly what I looking for. Thank you. Like

  • @MikeTheCook
    @MikeTheCook 4 месяца назад

    Love it. You're the best.

  • @Lidkovsky
    @Lidkovsky 7 месяцев назад

    golden content.

  • @memeMerge
    @memeMerge 10 месяцев назад

    I really liked it 👍👍

  • @Anilsinghchd
    @Anilsinghchd Месяц назад

    Great content

  • @user-jz8wx9op8j
    @user-jz8wx9op8j 7 месяцев назад

    excellent video very imformative guy

  • @thisstory
    @thisstory 4 месяца назад

    Love your videos

  • @rellamwahid6392
    @rellamwahid6392 2 месяца назад

    thank you for the video, i think you should add a cleanup function in the useEffect hook to cancel any ongoing requests when the component unmounts or when page changes (talking about the abort ).

  • @garikmelqonyan6011
    @garikmelqonyan6011 4 месяца назад

    Good job!

  • @hilkiahlavinier
    @hilkiahlavinier Год назад +3

    Thanks for this great video. Will you consider doing a follow up video that discusses the pros/cons of the various (or most popular) React fetch methods/options (e.g. Axios et al) and suggest their usecases (when to use one over the other)?

  • @capt_vj
    @capt_vj 4 месяца назад

    awesome
    !

  • @joshuac9142
    @joshuac9142 Месяц назад

    Perfect!

  • @rajnishkushx
    @rajnishkushx 5 месяцев назад

    Luv bro, from Bharat ❤

  • @harag9
    @harag9 8 месяцев назад

    Great video. The main different I do is with the abortController, I always put that in the cleanup return, which is what I've seen in several other videos on the same topic, it's interesting to see you doing it differently, both ways look like it will work just as well though.
    As for React Query you mentioned, do you plan on doing a tutorial on that?

    • @cosdensolutions
      @cosdensolutions  8 месяцев назад +1

      Yeah that's another way to do it! And the RQ video has been up for a while already ☺️

    • @harag9
      @harag9 8 месяцев назад

      @@cosdensolutions Ahh cool, still working my way through the videos. :) Really enjoying them .

  • @lynx7077
    @lynx7077 3 месяца назад

    I was trying to start the core of your project and i was keep getting an error. I believe i re-wrote it like 4 times.
    And then i realized something i never learned before during my Bootcamp: you have everything with tsx lol. What i was trying to do, was to write everything in jsx. So i looked it up and apparently there is a "create react with typescript" 😵‍💫 and now it shows everything. Learning something new every day. Althought i am still a bit confused between tsx and jsx and their usage

  • @hameeeed5992
    @hameeeed5992 13 дней назад

    Thanks

  • @kreten780
    @kreten780 7 месяцев назад

    For loading, I really like to use an object of properties like:
    const FETCH_STATUS = {IDLE: 'idle', LOADING: 'loading', ERROR: 'error', SUCCESS: 'success'};
    In that way I have a little bit more control over what I display on different occasions.

  • @user-tu1lx6vz9p
    @user-tu1lx6vz9p Год назад

    please make videos on doing project. your channel is growing since last time i commented doing great keep it up i am your subscriber

    • @cosdensolutions
      @cosdensolutions  Год назад

      How about livestreaming building an entire app from scratch, while also making it open source so anybody can contribute no matter the skill level? It'll be a great way to learn and build something real! And also, no worries, all videos will be available even after the live stream!

    • @user-tu1lx6vz9p
      @user-tu1lx6vz9p Год назад

      @@cosdensolutions yes thats good idea

  • @Riflijones01
    @Riflijones01 3 месяца назад

    You WILL want to watch another video on Fetching Data. Watch as many as you can because that will show you that there are other ways of doing things

    • @shashwatgupta2184
      @shashwatgupta2184 2 месяца назад

      Can you provide few links looks like you already seen some different ways

  • @haroonkhan-dy9bt
    @haroonkhan-dy9bt 11 месяцев назад +1

    Hey great tutorials will do a context api tutorial with fetching data and adding a cart functionality etc?

    • @cosdensolutions
      @cosdensolutions  11 месяцев назад +1

      Haven't gotten around to doing tutorials of actual features but yes, I will do! However I have already a tutorial on useContext 😁

  • @MuhammadWaqas-eb1uu
    @MuhammadWaqas-eb1uu 3 месяца назад

    Can I use hook useloader as async fucntion to fetch api data. That is optimize way and performance application increase.

  • @sehlanarslan1792
    @sehlanarslan1792 4 месяца назад

  • @aswineepratihari7897
    @aswineepratihari7897 5 месяцев назад

    21:38 abort control

  • @haiderjaafer8164
    @haiderjaafer8164 Год назад +2

    So useState is the Post a class previously you created so it here generic... Hope to clarify that... Thank

    • @cosdensolutions
      @cosdensolutions  Год назад

      Yes, I created a type of Post, with id and title. There are more properties from the API but since we only use those, I just created that type!

  • @dardaniaa0546
    @dardaniaa0546 4 месяца назад

    React Query makes this super easy

  • @aggranular
    @aggranular 5 дней назад

    If you don’t care about type of error you should use unknown, not any

  • @haiderjaafer8164
    @haiderjaafer8164 Год назад

    Really great keep going forward... But learning react in js not in type script

    • @cosdensolutions
      @cosdensolutions  Год назад

      that's fine! Just ignore the types then! But it would be useful to just be familiar with it, so that when you eventually do go to typescript (you should), you'll be more familiar!

  • @richardleungwoogabriel9317
    @richardleungwoogabriel9317 8 месяцев назад

    Great video! But I’m trying to get in your discord channel but I need an invite link. The link in the description doesn’t work for me

    • @cosdensolutions
      @cosdensolutions  8 месяцев назад

      it should work, or just manually go to discord.cosdensolutions.io

  • @JonathanKila
    @JonathanKila 4 месяца назад

    Any resource on data fetching and caching? Like why should I use React Query, SWR?

  • @maryambibi9687
    @maryambibi9687 6 месяцев назад

    Can you make one video in react life cycles ?

  • @abdalghanialbiek8999
    @abdalghanialbiek8999 5 месяцев назад

    I wish to play this sound when Eren meet Anni in all timelines

  • @piotrszczesniak
    @piotrszczesniak 4 месяца назад

    Hey Cosden! Thanks for the video. I never considered, or even never heard of, cancelling the racing condition. What I would normally do it is to disable the button that fires another call when the isLoading is true - it does the job 🚀. What do you think about my solution?

    • @cosdensolutions
      @cosdensolutions  4 месяца назад +1

      It would work, but then you're blocking the UI waiting for a response. What if the user changes their mind before the request returns? It's easier to let them change their mind and just cancel the pending request

    • @piotrszczesniak
      @piotrszczesniak 4 месяца назад

      @@cosdensolutions I agree with you. Actually, this is the first time I heard about the abortController(). I will keep this video as a reference when I develop sth that would need the abortController(). In my case, using isLoading to disable the UI for a second is a good enough option :)

  • @abhinasregmi9742
    @abhinasregmi9742 4 месяца назад

    I was wondering if we could add signal aborting in cleanup for useEffect instead at the top 🤔🤔 Will this work??

  • @erikreyes6093
    @erikreyes6093 9 месяцев назад +2

    Can you create a tutorial that shows the actual sql query that fetch records from database please.
    A plain sql query and sql that uses views and stored procedure please
    Thank you very much

    • @cosdensolutions
      @cosdensolutions  9 месяцев назад +1

      probably not for a while, I am focusing on react at the moment!

  • @user-cr1xn1td4j
    @user-cr1xn1td4j 10 месяцев назад

    please make a video about typescript, this new syntax sometimes shocks me

  • @faisalhussain7008
    @faisalhussain7008 4 месяца назад

    What if we abort the request inside the cleanup function?

  • @user-qr4vm2uo5q
    @user-qr4vm2uo5q Год назад +1

    What theme is this? Does anyone know?
    Thank you in advance 🙏🙏

  • @MarkDavid-so9up
    @MarkDavid-so9up 5 месяцев назад

    Which is better?
    const fn = async() => {}
    fn()
    Or
    (async(){})()

  • @kkatsavaros
    @kkatsavaros 4 месяца назад

    Thank you, but why i take back two responses,? These responses are the same.

    • @cosdensolutions
      @cosdensolutions  4 месяца назад

      Maybe because you're in strict mode and React fires all useEffects twice

    • @kkatsavaros
      @kkatsavaros 4 месяца назад

      Yes, exactly. Thank you very much.

  • @ABDULLAHKHALID-ix6db
    @ABDULLAHKHALID-ix6db 5 месяцев назад

  • @marcrichet2626
    @marcrichet2626 8 месяцев назад

    Bonsoir, What's your vscode theme please? :)

  • @bidsouravbest
    @bidsouravbest 4 месяца назад

    Pls make your codes on JS. PLS

  • @AyushManShrestha
    @AyushManShrestha 6 месяцев назад

    TypeError: posts.map is not a function
    {isLoading && Loading...}
    {!isLoading && (
    {posts.map((post) => {
    return {post.title};
    })}
    )}

  • @aggranular
    @aggranular 5 дней назад

    I like your video but got a big fat 👎 for as Posts[] antipattern. Should have at least mentioned that in real application you should validate the response a type safe schema library

  • @brandonhopkins3438
    @brandonhopkins3438 8 месяцев назад +3

    Big shame it's written in typescript. Still, transferred it to js and this was a great tutorial. Liked. Subscribed.

    • @cosdensolutions
      @cosdensolutions  8 месяцев назад +4

      No problem if you use JS, but my advice is to start thinking about TS because it will probably become the standard eventually!

    • @harag9
      @harag9 8 месяцев назад

      As Darius has said, I too would recommend picking up TS and learning that, it really does help.

  • @choppdick433
    @choppdick433 3 месяца назад

    Hi, I am an Angular developor is learning react. I love the abortControllorRef best practice you used handle the api paging part that let the later fetch to cancel the conducting fetch, which works same as the higher order mapping operater switchMap() in Angular Rxjs.
    I got one question here: there is another higher order mapping operater is concatMap() which is used to make sure that the later request always start after the previous request complete.
    so In your case it would be like:
    you increase the page to 2 (that will try to fatch the page 2 data) when the app is still loading the page 1,
    result will be: "the page 2 request will always start after the page 1 request complete"
    this can be useful for the case that the later request is depended on the previous reuqest.
    is there any way to implement that in React?

  • @josepdevillaret
    @josepdevillaret Месяц назад

    Can i contact you personally? I've got a project I'd like to get some React expertise in order to set a cart onto Framer (via React)

    • @josepdevillaret
      @josepdevillaret Месяц назад

      by connecting shopify backend onto framer frontend (via embeded code)

  • @jay-kv6wn
    @jay-kv6wn Год назад

    Great content