Full React Tutorial #19 - Handling Fetch Errors

Sdílet
Vložit
  • čas přidán 29. 08. 2024
  • Hey gang, in this React tutorial we'll handle any errors from the fetch request we made earlier & output any erors in the template.
    🐱‍💻 🐱‍💻 Course Files:
    + github.com/iam...
    🐱‍👤🐱‍👤 JOIN THE GANG -
    / @netninja
    🐱‍💻 🐱‍💻 My Udemy Courses:
    + Modern JavaScript - www.thenetninj...
    + Vue JS & Firebase - www.thenetninj...
    + D3.js & Firebase - www.thenetninj...
    🐱‍💻 🐱‍💻 Helpful Links:
    + HTML & CSS Course - • HTML & CSS Crash Cours...
    + Modern JavaScript course - • Modern JavaScript Tuto...
    + Get VS Code - code.visualstu...
    🐱‍💻 🐱‍💻 Social Links:
    Facebook - / thenetninjauk
    Twitter - / thenetninjauk
    Instagram - / thenetninja

Komentáře • 138

  • @unknownman1
    @unknownman1 Před 3 lety +95

    Not all heroes wear capes.Thanks Shaun for what u do for the community.

  • @Dexter101x
    @Dexter101x Před 3 lety +39

    Because of these tutorials, I think you have made me love ReactJs

    • @Kai-yu1km
      @Kai-yu1km Před 2 lety +4

      Me too. I hated React and tried to get motivated with it many times by watching a 2,5 hour video and doing the tic-tac game and reading, but no. It felt clumsy...
      One Saturday night I gave React on more chance, watching the Ninja's upload image firebase video, because it looked interesting. It was only 1:20 long. So cool! : )
      Net Ninja has already taught me so much! Big thanks Sir Ninja : )

  • @juanda531
    @juanda531 Před 2 lety +16

    Man, it's incredible the way you just explain some hard topics in a very easy way. Definetly, you're one of the best coding tutors I've ever seen. Congrats mate.

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

      I second this. The way he anticipates everything you might wonder is really great too. And every video is so jam-packed with valuable content that it's easy to pay attention!

  • @maskman4821
    @maskman4821 Před 3 lety +13

    I have learned a lot about conditional rendering, this is very important topic and used a lot/quite often with dynamic rendering in daily development, but with server side render/nextjs we dont need conditional loading. thank you Shaun for this terrific tutorial !!!

  • @nkpvg6805
    @nkpvg6805 Před 3 lety +8

    You are the best coding teacher ever and by the way your modern Javascript Course is really Epic and is rocking

  • @johnconnor9787
    @johnconnor9787 Před 3 lety +2

    The most understandable and concise video on Error Handling on CZcams

  • @yunger7
    @yunger7 Před 3 lety +21

    Shh! I'm an undercover ninja. 🐱‍👤
    I'm still watching your Firebase courses and will learn react next, I just came here to say thanks in advance :)

    • @NetNinja
      @NetNinja  Před 3 lety +3

      You're welcome 🐱‍👤🐱‍👤🐱‍👤

  • @dragonreborn2141
    @dragonreborn2141 Před rokem +4

    My man, this tutorial has everything I ever wondered about!
    Thanks a bunch!

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

    I learned a lot from the tutorial !!! This is basic, but everything you need to know to kick start a bigger project. Thanks Net Ninja

  • @leeoiou7295
    @leeoiou7295 Před 3 lety +1

    you are the best teacher on youtube. Your explanation is so clean.

  • @manuelvaal1257
    @manuelvaal1257 Před měsícem

    cleaning my concepts thanks to you. appreciation from here.

    • @NetNinja
      @NetNinja  Před měsícem

      Great job! :) thanks for watching

  • @Bestbee_graphics
    @Bestbee_graphics Před 19 dny

    This guy is the best Coach💯

  • @donmikkodanm.olmillo8154
    @donmikkodanm.olmillo8154 Před 3 lety +4

    Hello @TheNetNinja
    , I really love your courses and thank you so much for providing free knowledge to all of us aspiring web developers 🎊🎉.
    I would like to ask if what's the difference between using the fetch API and axios? I'm currently using axios while following your tutorial and sometimes It's hard for me to follow especially this handling fetch error.
    Also I would like to suggest of a course of you guiding us on how to use axios pleeease? thank you!

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

    Amazing, refreshing way of explaing by Net Ninja, keep up the good work. :)👌👌

  • @r34ct4
    @r34ct4 Před rokem

    This tutorial is a godsend and you're an amazing teacher. I just keep hearing "error" as a DJ scratching noise XD

  • @thedvlpr
    @thedvlpr Před 3 lety +1

    The best tutorial ever, thank you so much, Shaun!

  • @Ismail-hq8cq
    @Ismail-hq8cq Před rokem

    The time I am writing this comment I am watching the Ad on this video just to help you. I appreciate you hard work trying to help us for free. Thank you so much.

  • @haha78923
    @haha78923 Před 2 lety +1

    This is so helpful. Thank you for making these tutorials!

  • @ChristopherPhillipsDev
    @ChristopherPhillipsDev Před rokem +1

    Since we always want to setIsLoading(false) after the fetch completes (whether success or failure), it's best to chain a .finally(() => setIsLoading(false)) to the end

  • @vijaytembugade
    @vijaytembugade Před 3 lety +6

    Thank you so much for this series! I request you to make MERN series on youtube or MERN udemy course! We want that! Please!

  • @griimaze7049
    @griimaze7049 Před 3 lety

    for those who are using async like me, use 'try and catch' like this.
    useEffect(async () => {
    try{
    const res = await fetch('localhost:8000/blogss');
    if(!res.ok){
    throw Error("could not fetch data for that resource");
    }
    const data = await res.json();
    setError(null);
    setBlogs(data);
    setIsPending(false);
    }
    catch(err){
    setIsPending(false);
    setError(err.message);
    }
    }, []);

  • @rafiashan8389
    @rafiashan8389 Před 24 dny

    Thank you so much Shaun.

  • @vuthyyt9977
    @vuthyyt9977 Před rokem

    It was already 3 years but it still do help 😘😘

  • @dannyman2200
    @dannyman2200 Před 3 lety +1

    I've never enjoyed an error handling tutorial until now 😉

  • @sfybskot
    @sfybskot Před 3 lety

    The best tutorial ever! I've got a ton of answers! Thanks soooooo much! I'm your follower forever!

  • @sammyg123456654565
    @sammyg123456654565 Před 2 lety +4

    Shaun, thanks for the great tutorial. Just out of curiosity, rather than using the state for removing the loading message when there is an error, could you instead do the following: { !error && isLoading && Loading... }
    I tested it and it worked, but want to know if that isn't a best practice?
    Thanks!

    • @clarckkim
      @clarckkim Před rokem

      i huess he wants to show it with more gymnastics so people know the possibilities

  • @nikolozmacharashvili1212

    thank you for making my dreams partly come true. I also love London.!!! rip grandma!!!

  • @pranavdhamanage
    @pranavdhamanage Před 3 lety

    The explanation is really awesome!! Love it!!!

  • @fightforfitness2256
    @fightforfitness2256 Před měsícem

    Thanks man. This is awesome.

  • @dev-290ch5
    @dev-290ch5 Před 9 měsíci

    Note that when Strict Mode is enabled, rendering occurs twice even though you specified empty array for useEffect dependency - hence you will see the error message being displayed twice.
    Maybe it happens for the new version of React.

  • @leon8dev
    @leon8dev Před 3 lety

    Thank you for every thing.Love u SO much Shaun

  • @parthipankalayini8
    @parthipankalayini8 Před 3 lety +2

    Awesome please do more React.js projects.

  • @woofcode3383
    @woofcode3383 Před 2 lety

    Awesome as always. Super appreciate you!

  • @igorr4682
    @igorr4682 Před 3 lety

    U should add finally and set pending there to false. It's good practice to add that block for any clean up

  • @Lok1goD
    @Lok1goD Před rokem

    God bless you! Thank for you work! 😍

    • @NetNinja
      @NetNinja  Před rokem

      Very welcome! Thanks for watching :)

  • @siddhantgupta6719
    @siddhantgupta6719 Před 3 lety +12

    I follow your tutorials avidly. I do have a question though, shouldn’t “setIsPending(false)” be kept in “finally” instead of “then” and “catch”?

    • @WorstDeveloper
      @WorstDeveloper Před 3 lety +2

      Yes, that would be a more appropriate way of doing it since you don't have to call it in two places.

  • @louis_dev6981
    @louis_dev6981 Před 3 lety

    npx json-server --watch data/db.json --port 8000
    ^ for those of you who have been following the tutorial series and don't want to type out the command to start the json server

  • @mohammadabdelrahman786

    This video was more of a js tutorial rather than react 😅 I'm gonna pause the list and review promises

  • @peytube
    @peytube Před 5 měsíci

    So clear , Bravo

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

    For handling isPending, we can add finally block in Promise.then chain

  • @HamedQaderi
    @HamedQaderi Před 3 lety

    You can set isPending in a finally block instead of repeating it in both then and catch.

  • @ROHANPAGAR
    @ROHANPAGAR Před 2 lety

    I almost spend a month to learn React but I couldn't , By just watching your video in a day I learn a lot

  • @CjammWest
    @CjammWest Před 3 lety

    You are the best. Please keep up these contents.

  • @chtiouimoncer6092
    @chtiouimoncer6092 Před rokem

    U are a legend thanks ! ♥

  • @kashifmehmood4399
    @kashifmehmood4399 Před 2 lety

    very good sir you are the best

  • @dipstickjimmy789
    @dipstickjimmy789 Před 3 lety

    Fookin champion as usual Shaun - is dealing with CORS issues beyond the scope of this series?

  • @hamidr8391
    @hamidr8391 Před 2 lety

    Thanks a lot, this is life saving.

  • @thicctamil204
    @thicctamil204 Před 3 lety

    Fam i love What your doing but the Ads are Killing me out here, still a massive fan of your work.

  • @GAJARI
    @GAJARI Před 2 lety

    Cool. My react ecommerce website is exactly facing same issue, thank you for your tips

    • @NetNinja
      @NetNinja  Před 2 lety

      You're welcome! Hope it helps :)

  • @marimuthur9456
    @marimuthur9456 Před 2 lety

    Really superb👌🏼👌🏼👌🏼

  • @i_am_human_being5570
    @i_am_human_being5570 Před 3 lety

    Very very easy and useful!!!

  • @GiancarloCarccamo
    @GiancarloCarccamo Před 2 lety

    great video man

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

    Great video!, btw a question, how can status be true (200) with cors error? @3:22, like it should throw an error due to cors right?

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

    Very precise

  • @sertacekici3342
    @sertacekici3342 Před 3 lety

    I'm waiting for next video. Thank you so much

  • @mayarjaba6731
    @mayarjaba6731 Před 2 lety

    waow what a leason
    thanks shaun ♥♥♥

  • @kennanfattah1468
    @kennanfattah1468 Před 3 lety

    Giving the best, always 🎊👍

  • @_pve
    @_pve Před 3 lety

    Great tutorial keep it up my friend!

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

    This is amazing

  • @karinljunggren22
    @karinljunggren22 Před 3 lety

    Thank you! Very useful for me

  • @rickvian
    @rickvian Před 3 lety +1

    you should axios

  • @janvar6914
    @janvar6914 Před 3 lety +4

    Hi, Shaun, I have a question/problem. At the very end of this lesson I wanted to know if it's working so I kept switching between correct and incorrect endpoints (blogs vs. blogssss) and I noticed that on just saving the file it showed both the error message and the fetched data at the same time, which is not okay. To be sure I copied your code from the repo and it behaved the same way, so I think I eliminated any misspellings/mistakes/typos/omissions in my code. I am a learner, help me out, sensei 🙂.

    • @AndriusLauFullstack
      @AndriusLauFullstack Před 3 lety +4

      Yes, that's why you need to setError(null) in the second .then block, and setBlogs(null) in .catch block. Now rendering will display only one post or only an error message.

    • @faizansaleem9905
      @faizansaleem9905 Před 2 lety +1

      @@AndriusLauFullstack Thanks!

  • @akashnegi913
    @akashnegi913 Před 3 lety

    Good as Always . Hi from India : )

  • @rohan1765
    @rohan1765 Před 3 lety +1

    Thanks alot

  • @deonvisser2480
    @deonvisser2480 Před 2 lety

    Appreciated!

  • @chinmayghule8272
    @chinmayghule8272 Před rokem

    If you're using async/await instead of promise chaining, note that fetch will not throw an error on a 404 response. A 404 response is considered a valid response so the code in the try block won't throw an error. You will have to manually check with an if statement, before you try to extract using the .json() method.
    This is how I did in my try block:
    ***
    const users = await fetch(URL);
    if(users.status !== 200) {
    throw Error('failed to fetch');
    }
    usersData = await users.json();
    ***
    I'm going to link the stackoverflow link for this problem by replying myself. YT seems to be deleting comments with links.

  • @shahryarsaeed6047
    @shahryarsaeed6047 Před rokem

    Just Amzing❤‍🔥

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

    very useful!!

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

      Glad it was helpful! :)

  • @craig-scott
    @craig-scott Před 2 lety

    Champion!

  • @RandomGuy-jv4vd
    @RandomGuy-jv4vd Před rokem

    Just briliant

  • @tarikmujcic2966
    @tarikmujcic2966 Před rokem

    Why do I always have double console.logs in the console. When I log anything to the console that is in useEffect I have it logged 2 times in the console.

  • @zhakshylykkozhobekov8683

    thanks

  • @liewweijun6728
    @liewweijun6728 Před 3 lety

    how is the res value includes the type of Cors? Does this mean it allows other origin access control?

  • @hassanhafiz44
    @hassanhafiz44 Před 3 lety

    Thanks!

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

    At 1:26, you stopped the server, then you reloaded the page, and it appeared ( Loading..... )The error also appeared via Console, but the question is, how did it appear if you stopped the server? The page should not work in the first place because you stopped the server, and this is what happened to me

  • @Levoxel
    @Levoxel Před 3 lety

    Hello Shaun, great channel, lots of pedagogy here. Why don't you make a Patreon or Tipeee page ?

  • @jorgearraga
    @jorgearraga Před 3 lety

    The better option to manage states (loading, ready, error) is use numeric state

    • @exodion4173
      @exodion4173 Před 3 lety

      Can u give an example for this ? Im having trouble wrapping my head around this..

  • @moe3551
    @moe3551 Před 2 lety

    so the throw Error function automatically puts the error message we type in, inside the error hook we defined in the beginning ? if so why is this the case ?

  • @aleksejvidic6897
    @aleksejvidic6897 Před 3 lety

    One question when we use iterators and generators in programming and for what we use them?

  • @indobikelovers7158
    @indobikelovers7158 Před rokem

    i'm using async ,but cant make throw error running with 'try and catch'. Anyone can help?

  • @Lovatic1boy
    @Lovatic1boy Před 3 lety

    dude i love ur accent

  • @SaiKiran-xc2ge
    @SaiKiran-xc2ge Před rokem

    My fav

  • @nguyennam5056
    @nguyennam5056 Před 2 lety

    thank youuuu.

  • @asharma741
    @asharma741 Před 3 lety

    Hi Shaun I got a quick question there, at last, you use setError(null) again in then block but is there any need there because the state of error is already set to null, Can I just know what is the idea of setting state again to null?

    • @AndriusLauFullstack
      @AndriusLauFullstack Před 3 lety +2

      If you do not refresh the page, but instead change URL to link with 404 status and then try again with the right URL, you will see that component renders, but it will have the error message. When refreshing, this doesn't appear.

  • @phantom7132
    @phantom7132 Před 3 lety

    I'm a little bit confused. Why do you check if res.ok does not exist while pinging a wrong endpoint already results in an error that gets handled within the catch-block?

    • @codewithfarhad8594
      @codewithfarhad8594 Před 3 lety +1

      when you throw Error in a then() block, it skips all upcoming then() blocks and goes directly to the catch block. that is why when pinging wrong endpoints, the code sees that the res.ok == false and then error is thrown which ultimately gets caught in the catch block and we see the error get render due to our setup of error state.

    • @phantom7132
      @phantom7132 Před 3 lety +1

      @@codewithfarhad8594 Thanks, I got it now! I assumed that it skips all upcoming then-blocks by default.

  • @krishnaTiwari851
    @krishnaTiwari851 Před rokem

    iam confused what we have to do while using axios on that code if(!res.ok)................can you reply me ninja the pro

  • @kundogb
    @kundogb Před rokem

    You saved my ass today

  • @ridl27
    @ridl27 Před 3 lety

    ty

  • @intensiveadvancedmath5281

    7:12 Error message doesn't pop up even not inser setError(null). Is it by default? Actually we don't have to put setError(null). Who knows, answer please.

    • @rcinos
      @rcinos Před 5 měsíci

      Know, it is a bit late, so here is an example:
      When you sent a request to a server and it responded with a rejection, it would be saved in the state. And when you decided to send another request to the server and it responded without any errors, your error state would indicate the opposite

  • @sharriceowens913
    @sharriceowens913 Před 3 lety

    my control + C is not working to cancel out the data

  • @user-zp9yx4lw8k
    @user-zp9yx4lw8k Před 2 lety

    Help, please. By some reason instead of server's response 200 I get 304. I got 200 just once , the first time the db loaded, I guess... I tried to fix it and even started the tut again. But it doesn't help. And it's crutial, as further on there will be error abortion and it always aborts ((( because catches this 304 response. Or I just can't figure it out... I understand that it's somehow connected to my cookies-settings, maybe. I would really appreciate if someone finds time to reply to me.

  • @ckatke
    @ckatke Před 3 lety

    Here we go

  • @helpingselflearner4391

    Please do a dashboard project with latest react... With hooks not classes and redux and firebase..... Please please please

  • @syncr7433
    @syncr7433 Před 2 lety +1

    do u have a community in discord?

  • @AMANKUMAR-qg7pf
    @AMANKUMAR-qg7pf Před 3 lety

    Can anyone help? I am getting "Error is not a function".

  • @4541047
    @4541047 Před 3 lety +1

    But why not async await?

  • @demogyani2419
    @demogyani2419 Před 3 lety

    What happens to 4xx errors? Will it go to catch block?

    • @EnesKab
      @EnesKab Před 2 lety

      Yes it will. If you want to catch specific type of error (maybe to customize the error message) you can use response.status in your if statement. Like if (response.status !==200)

  • @lenardesguerra4548
    @lenardesguerra4548 Před 3 lety

    Ang galiingggggggggg mo magturoooooooo!

  • @camarafasseli0
    @camarafasseli0 Před 2 lety

    C'est joli