Full React Tutorial #19 - Handling Fetch Errors

Sdílet
Vložit
  • čas přidán 10. 01. 2021
  • 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/iamshaunjp/Complet...
    🐱‍👤🐱‍👤 JOIN THE GANG -
    / @netninja
    🐱‍💻 🐱‍💻 My Udemy Courses:
    + Modern JavaScript - www.thenetninja.co.uk/udemy/m...
    + Vue JS & Firebase - www.thenetninja.co.uk/udemy/v...
    + D3.js & Firebase - www.thenetninja.co.uk/udemy/d...
    🐱‍💻 🐱‍💻 Helpful Links:
    + HTML & CSS Course - • HTML & CSS Crash Cours...
    + Modern JavaScript course - • Modern JavaScript Tuto...
    + Get VS Code - code.visualstudio.com/
    🐱‍💻 🐱‍💻 Social Links:
    Facebook - / thenetninjauk
    Twitter - / thenetninjauk
    Instagram - / thenetninja

Komentáře • 132

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

    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 4 dny

      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 !!!

  • @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 🐱‍👤🐱‍👤🐱‍👤

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

  • @dragonreborn2141
    @dragonreborn2141 Před rokem +4

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

  • @kristine.wakeup
    @kristine.wakeup Před 13 dny

    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

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

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

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

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

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

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

  • @pranavdhamanage
    @pranavdhamanage Před 3 lety

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

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

  • @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!

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

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

  • @woofcode3383
    @woofcode3383 Před 2 lety

    Awesome as always. Super appreciate you!

  • @leon8dev
    @leon8dev Před 3 lety

    Thank you for every thing.Love u SO much Shaun

  • @jaisgeorge1166
    @jaisgeorge1166 Před 10 dny

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

  • @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!

  • @i_am_human_being5570
    @i_am_human_being5570 Před 2 lety

    Very very easy and useful!!!

  • @_pve
    @_pve Před 3 lety

    Great tutorial keep it up my friend!

  • @kennanfattah1468
    @kennanfattah1468 Před 3 lety

    Giving the best, always 🎊👍

  • @nikolozmacharashvili1212

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

  • @CjammWest
    @CjammWest Před 3 lety

    You are the best. Please keep up these contents.

  • @karinljunggren22
    @karinljunggren22 Před 3 lety

    Thank you! Very useful for me

  • @sertacekici3342
    @sertacekici3342 Před 3 lety

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

  • @hamidr8391
    @hamidr8391 Před 2 lety

    Thanks a lot, this is life saving.

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

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

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

    Awesome please do more React.js projects.

  • @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!

  • @chtiouimoncer6092
    @chtiouimoncer6092 Před rokem

    U are a legend thanks ! ♥

  • @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.

  • @mayarjaba6731
    @mayarjaba6731 Před rokem

    waow what a leason
    thanks shaun ♥♥♥

  • @marimuthur9456
    @marimuthur9456 Před 2 lety

    Really superb👌🏼👌🏼👌🏼

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

  • @GiancarloCarccamo
    @GiancarloCarccamo Před 2 lety

    great video man

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

  • @kashifmehmood4399
    @kashifmehmood4399 Před 2 lety

    very good sir you are the best

  • @akashnegi913
    @akashnegi913 Před 3 lety

    Good as Always . Hi from India : )

  • @Lok1goD
    @Lok1goD Před rokem

    God bless you! Thank for you work! 😍

    • @NetNinja
      @NetNinja  Před rokem

      Very welcome! Thanks for watching :)

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

  • @aleksejvidic6897
    @aleksejvidic6897 Před 3 lety

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

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

  • @dipstickjimmy789
    @dipstickjimmy789 Před 3 lety

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

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

    This is amazing

  • @rog0079
    @rog0079 Před 4 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?

  • @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?

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

    So clear , Bravo

  • @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.

  • @deonvisser2480
    @deonvisser2480 Před rokem

    Appreciated!

  • @hassanhafiz44
    @hassanhafiz44 Před 3 lety

    Thanks!

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

    Thanks alot

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

    Just briliant

  • @GAJARI
    @GAJARI Před rokem

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

    • @NetNinja
      @NetNinja  Před rokem

      You're welcome! Hope it helps :)

  • @dev-290ch5
    @dev-290ch5 Před 6 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.

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

    Very precise

  • @zhakshylykkozhobekov8683

    thanks

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

    very useful!!

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

      Glad it was helpful! :)

  • @HamedQaderi
    @HamedQaderi Před 3 lety

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

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

    Champion!

  • @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 2 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!

  • @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 ?

  • @thicctamil204
    @thicctamil204 Před 2 lety

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

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

    Just Amzing❤‍🔥

  • @ridl27
    @ridl27 Před 3 lety

    ty

  • @mohammadabdelrahman786

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

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

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

  • @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.

  • @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 2 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.

  • @kundogb
    @kundogb Před rokem

    You saved my ass today

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

    My fav

  • @mustafakhu904
    @mustafakhu904 Před 5 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

  • @nguyennam5056
    @nguyennam5056 Před 2 lety

    thank youuuu.

  • @Lovatic1boy
    @Lovatic1boy Před 3 lety

    dude i love ur accent

  • @ckatke
    @ckatke Před 3 lety

    Here we go

  • @indobikelovers7158
    @indobikelovers7158 Před rokem

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

  • @Levoxel
    @Levoxel Před 3 lety

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

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

  • @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.

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

    you should axios

  • @sharriceowens913
    @sharriceowens913 Před 3 lety

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

  • @jcavenue
    @jcavenue Před 2 lety

    auto subscribe hahaha

  • @user-zp9yx4lw8k
    @user-zp9yx4lw8k Před rokem

    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.

  • @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.

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

    do u have a community in discord?

  • @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);
    }
    }, []);

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

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

  • @camarafasseli0
    @camarafasseli0 Před rokem

    C'est joli

  • @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..

  • @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 2 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

  • @helpingselflearner4391

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

  • @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)

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

    But why not async await?

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

  • @lenardesguerra4548
    @lenardesguerra4548 Před 3 lety

    Ang galiingggggggggg mo magturoooooooo!

  • @deathineyes
    @deathineyes Před rokem

    I dont like this method

  • @Mahmudulhasan-ts5hm
    @Mahmudulhasan-ts5hm Před 3 lety

    thanks