Тёмный

Full React Tutorial #17 - Fetching Data with useEffect 

Net Ninja
Подписаться 1,6 млн
Просмотров 349 тыс.
50% 1

Hey gang, in this React tutorioal we'll see how to make a fetch request for data using the useEffect hook.
🐱‍💻 🐱‍💻 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

Опубликовано:

 

26 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 234   
@rafiashan8389
@rafiashan8389 Месяц назад
I can't believe someone can explain this well in such a short video. Ninjas were always my favs since childhood.
@amanpreetsinghbawa1600
@amanpreetsinghbawa1600 3 года назад
The best part of tutorials is you have them backed by the course files. Very well managed.
@ccd-94
@ccd-94 3 года назад
Bro you're a life saver, I am a bootcamp student who almost quit because I can't keep up with react, now here, I am getting hope to continue, I press like and watch all the ad to show my support to your channel, please stay awesome sir, God bless!
@86Illa
@86Illa 3 года назад
Thank you so much for this course! Your way of explaining is extremely clear - I have used React for a while now, but at times things still get confusing. You make everything extremely clear :)
@once-upon-a-time-7
@once-upon-a-time-7 2 года назад
i love you :)
@equim7363
@equim7363 7 месяцев назад
@@once-upon-a-time-7 no simping please
@freeyourmind7538
@freeyourmind7538 5 месяцев назад
​@@equim7363 I love you 💓
@aliaksandrhn1
@aliaksandrhn1 2 года назад
5:40 You could have easily corrected the error by using an empty array rather than null as an initial state: const [blogs, setBlogs] = useState( [] );
@KingstonFortune
@KingstonFortune 2 года назад
thanks for this tip! glad I took a glance at the comments and found yours as number 1
@pratyushpurohit
@pratyushpurohit 2 года назад
He could but his point was to teach us a new concept. Glad he didn't use []
@ziaurrahman9529
@ziaurrahman9529 Год назад
thanks!
@seclvded
@seclvded Год назад
damn, u da mvp. but i also like how he explained it (in case u run to his exact problem)
@kavinsanthosh1306
@kavinsanthosh1306 Год назад
yeah bro. but why it is not preventing the rendering of blog list. when theoritcally it should?
@conan_ft
@conan_ft 3 года назад
I love all your tutorials, you explain it all so clear!
@NetNinja
@NetNinja 3 года назад
Thank you so much! :)
@tomaszswiatek4612
@tomaszswiatek4612 3 года назад
yep i agree:), and I'm still up to date with this awesome n1 tutorial :)
@melancholicmuse
@melancholicmuse 2 года назад
This course is my first introduction to React and i'm enjoying the lessons so much
@DigitalMonsters
@DigitalMonsters 3 года назад
Set blogs initial state to an empty array if you don't want to have to conditionally render with the double ampersand. Since an empty array is still a valid map object.
@karanparmar4318
@karanparmar4318 3 года назад
True, But I think he did this purposefully to make us understand about conditional rendering.
@reactdev7871
@reactdev7871 3 года назад
I was just gonna say that good job buddy as I made a real blog with an API here and made a search box on the navbar that can search and filter the blogs so was confused with this null issue so I find out that using empty array fix the issue :D
@defaultdefault812
@defaultdefault812 2 года назад
Need to be careful that no .length method is evaluated.
@sabinoramirez2600
@sabinoramirez2600 2 года назад
problem with this approach is the title will display even if the array is empty. This looks ugly in real world application
@ericmomoh7927
@ericmomoh7927 2 года назад
@@reactdev7871 please can I see the code on how you made the search bar?
@yangliu3754
@yangliu3754 3 года назад
Great solution to deal with the null! After the data is fetched and the setBlogs is invoked, React re-renders the whole component, and this is why when the blogs value changes the (blogs && ) part runs again.
@nikokalandadze9675
@nikokalandadze9675 Год назад
Thank you, king behavior
@xinglingoriginal5428
@xinglingoriginal5428 2 месяца назад
I can't believe you explain so clear and easily to understand, I'm so glad you exist =). Thank you man
@NetNinja
@NetNinja 2 месяца назад
Aha, thanks for the lovely comment :) and for watching!!
@samirprasla1770
@samirprasla1770 3 года назад
Thank You so much for your such hard work on all of your tutorial videos, you are very polite and honest teacher !! too the point tutorials all of them are very well explained....!!
@NetNinja
@NetNinja 3 года назад
Thank you so much! 😃
@ayushdudhani
@ayushdudhani Год назад
Thanks alot ! I was using axios while getting data!! It was for my hackathon, used same way as you shown!! it worked!! THANKS ALOT MAN !!!!!!
@LandrieMarManait
@LandrieMarManait 2 месяца назад
axios is way better and simplier than using fetch() function for me
@towseefahmed6328
@towseefahmed6328 3 года назад
ur simply excellent ... explanation is outstanding... thanks a lot for this excellent content
@googleaccount7252
@googleaccount7252 2 года назад
Tip for those struggling, make sure that you have two terminals running, one for react the other for db. and make sure you .db file is saved.
@user-ct5oo3do7b
@user-ct5oo3do7b 3 года назад
Async version: useEffect(() => { async function fetchBlogs() { let response = await fetch('localhost:8000/blogs'); response = await response.json(); setBlogs(response); } fetchBlogs(); }, []);
@CodeRCreatives
@CodeRCreatives 3 года назад
You are really very awesome teacher very nicely explained all the concepts of reacts.
@madannikalje760
@madannikalje760 3 года назад
Thank you so much this helped me alot ngl I was stuck in that error for long time I wasn't able display my fetch data
@youngun550
@youngun550 2 года назад
I would absolutely love it if you did a brief tutorial on how to externalise an async await code block. I've managed something close, but I still want to abstract away.. Thanks for the awesome content! Really enjoy your videos, by far the most helpful I've come across :P
@parthdesai9751
@parthdesai9751 Год назад
at this point, you could probably ask chatgpt
@samtakahashi5602
@samtakahashi5602 2 года назад
THIS. Was working on an Update/Edit function to finish off this series and was stuck for a few hours wondering why my data properties were returning null... Thank you for this fantastic course!
@princearthur5532
@princearthur5532 2 года назад
Wow you are great at explanations i have tried several tutorials but this tops it.
@NetNinja
@NetNinja 2 года назад
Great to hear :) thanks Prince!
@ScottRocke
@ScottRocke 3 года назад
Thank you for the clear and concise video. Definitely helped unblock some brain cells.
@gorkemgok9313
@gorkemgok9313 10 месяцев назад
Man you deserve a prize or something as the best youtube code teacher.
@NetNinja
@NetNinja 10 месяцев назад
@henrydinh285
@henrydinh285 2 года назад
OMG! Glad i saw this video. The logical && solved my problem i had for days as im learning react and hooks. Thanks Shaun ❤
@olorundareagnes934
@olorundareagnes934 3 года назад
I must say that you are a superb teacher. Well done Shaun!
@Eldalion99999
@Eldalion99999 3 года назад
single most useful react video in whole youtube......do you have any idea how long it took me to find all these different pieces of information condensed in this video ??? Especially the 2x .then part....jezus the things I tryed to achieve that. I wish I had this 6 months ago.....
@eylemgokdemir
@eylemgokdemir 2 года назад
BEST json server tutorial. THANK YOU!
@akashnegi913
@akashnegi913 3 года назад
Your Videos are right on Time So are we , Thank You :)
@azeezalsafadi901
@azeezalsafadi901 Год назад
I learned a million things from this lesson Thank you The Net Ninja!!
@NetNinja
@NetNinja Год назад
You're very welcome Azeez! :)
@andrewpenny1285
@andrewpenny1285 Год назад
You solved a problem for me that has been very discouraging. I forgot to set initial stage to null and also forgot about the conditional rendering. I was getting the error about mapping over an undefined array. Thank you again and again!
@staplepin8099
@staplepin8099 3 года назад
Best teacher
@bobhammell6308
@bobhammell6308 Год назад
Very consise tutorials, great for picking up the concepts. Thank you!
@filmbotreviews
@filmbotreviews Год назад
Thanks so much for this video, helped me with the problem I was having.
@NetNinja
@NetNinja Год назад
Glad it helped :)
@AlDasturchi
@AlDasturchi 4 месяца назад
The most useful account ever
@NuclearAmouri
@NuclearAmouri 2 года назад
5:15 Couldn't we just initialize `blogs` to an empty array? const [blogs, setBlogs] = useState([]);
@anony4142
@anony4142 8 месяцев назад
Thanks for the explanation, but I have a simple question why we did not use asynchrounous function so we won't render anything till the blogs are returned from the fetch ?
@vidmis224
@vidmis224 3 года назад
You are life saver mate!!
@martiananomaly
@martiananomaly 2 года назад
The use of && was pretty smart.
@billpapas3054
@billpapas3054 3 года назад
What a god explanation and teach!!!!! Adtonishing buddy!! Ty
@levantinian02
@levantinian02 7 месяцев назад
Thanks for the series. Question here: Why the blogs didn't get set to the value being returned from the fetch? Shouldn't the return statement wait until the fetch function finishes since it uses promises and everything should wait till it finish its work? And like that the value of blogs wouldn't be null. I want explanation to what happened :)
@RodRavenpictures
@RodRavenpictures 5 месяцев назад
The goat. (period)
@RameenFallschirmjager
@RameenFallschirmjager 3 года назад
I know this comment is the most irrelevant comment ever, but I couldn't help myself! I was watching your Vue udemy course and I reached the section that you were teaching how to emit events. I am familiar with this concept, because I learnt it when I was studying game development with godot engine. I got so excited for this "flash of enlightenment!" that I needed to show my gratitude somehow! This incident proves that it's important to expand your knowledge and how knowing some stuff in other areas helps to solidify your skills and knowledge as a whole. And I suggest everybody does it, I mean to try various stuff, it's good for your health!
@NetNinja
@NetNinja 3 года назад
Thanks so much Rameen :)
@RameenFallschirmjager
@RameenFallschirmjager 3 года назад
@@NetNinja Anytime my master ninja!
@elora2334
@elora2334 3 года назад
Thank you, its so clear with your explain !
@monson2048
@monson2048 Год назад
Amazing content! Love your explanations!
@dineshrout2527
@dineshrout2527 3 года назад
Love the way you teach 🙏
@Nova-cid
@Nova-cid Год назад
Hey! I found this video very late after i stepped into development . I wish i found about this channel earlier. Thank you so much for providing this great information. Also, we set the initial state for the blog data as 'null' . Could we use an empty array for that as well ? That way , i didn't get the error 'cannot read map of null'
@jerrerock
@jerrerock Месяц назад
thank you sir! You helped me a lot.
@AbrahamWilson
@AbrahamWilson 3 года назад
Dude your tutorials are excellent, seriously enjoying the content. Could you please make a course where you show how to make CRUD apps using the MERN stack.
@fatihkaya6817
@fatihkaya6817 3 года назад
could you show how do we implement fetching data in backend and passing the data via Express routes to frontend? This would be very helpful simple full stack implementation
@JayPatel-qh1ux
@JayPatel-qh1ux Год назад
did you get the answer??
@khedubaba
@khedubaba 3 года назад
Pure and sweeet knowledge.
@Pupu._
@Pupu._ 3 года назад
clear explanation as always! :)
@sheeshaliosama5837
@sheeshaliosama5837 Год назад
I did the same thing you did but it keeps giving me errors ('blogs.map is not a function') and (Failed to execute 'json' on 'Response': Illegal invocation) can you explain the problem and how to solve it please
@x9wozz
@x9wozz 3 года назад
Can't wait for more :D Also will you explain why we ended up using hooks instead of class components? I suppose it's because of things like mentioned before useEffect hook and more. Though I'm neither familliar with react class components so I don't know if I'm not wrong here
@maskman4821
@maskman4821 3 года назад
If you are new to react, just learn hooks, always go with the latest technologies !
@x9wozz
@x9wozz 3 года назад
@@maskman4821 but the thing is that most companies still use things that were written before hooks update and that's often the case that you have to deal with legacy-ish code. That's why learning class components is relevant for me, since I'm aiming to transfer from just being frontend dev to react dev*. *from what I've noticed there's a lot more of corpo work as frontend dev than actual programming as react dev if that makes sense.
@sirusThu
@sirusThu 2 года назад
Amazing explanation. thanks
@rogeclash2631
@rogeclash2631 Год назад
many thanks for this , it is very clear
@NetNinja
@NetNinja Год назад
Thanks Roge. Glad to hear that!
@Ishant56
@Ishant56 2 года назад
Wow amazing Tutorial Thank You ❤️
@Rose-93
@Rose-93 2 года назад
Thank you fo such a clear explanation :)
@maskman4821
@maskman4821 3 года назад
Thank yoy Shaun for this tutorial, I have learned a valuabke lesson which is conditional template, now I finally understand why and how { blogs && } works, it feels so good to comprehend the reason behind this !!!
@olgapolskolg1112
@olgapolskolg1112 3 года назад
Thanks a lot, you helped me to solve a problem
@rahulsriram6295
@rahulsriram6295 3 года назад
If you take the initial value as Empty array instead of null, it works too
@maskman4821
@maskman4821 3 года назад
cool !
@KehindeEnirayetan
@KehindeEnirayetan Год назад
I used both approach but was unable to see any blog displayed.
@KerishaStewart
@KerishaStewart Год назад
Your videos are amazing!
@NetNinja
@NetNinja Год назад
Thank you so much! :)
@Dgrayfit
@Dgrayfit 2 года назад
In regards to the 6:00 minute mark. So the idea with the {blogs && ... } code here is that because we have initially set the state of blogs to be null we will initially get blogs to evaluate as false, so the logical && won't fire since both conditions must be true. However, we 'react' to the new render by updating the state of blogs in the useEffect() hook, which then makes blogs evaluate to true and our component evaluate to true. I think I am following, but if anybody can spot a gap in my understanding please jump in.
@andrewpenny1285
@andrewpenny1285 Год назад
You are correct, but the conditional rendering wouldn’t be so important if it wasn’t for the map method being called on the data in the other component. Mapping over undefined data will throw an error and the app will fail to load anything at all. Basically, we are waiting until the data has arrived before we start trying to pass it around or render it.
@johnsmith-oy9kx
@johnsmith-oy9kx 3 года назад
Excuse me if I'm asking too much, please do a 'react native 2021' tutorial in 'net ninja' style, it will help us a lot. I know there are a lot of react native courses out there but we like it in net ninja style - simple, sharp and comprehensive. PLEASE...
@amjadaliafridi1023
@amjadaliafridi1023 Год назад
it has been a superb journey so for
@NetNinja
@NetNinja Год назад
Awesome to hear that Amjad!
@LorandPalfalvi
@LorandPalfalvi 2 года назад
Thank you! I would have loved it if you would have used async/await instead of then. Almost nobody uses .then anymore.
@AngryBacteria
@AngryBacteria Год назад
I now know how to only display something when the useFetch has the data from the json server. But how would I go about only doing a certain action if the json object is loaded? For example I only want to fill an array once the data is loaded but I dont know how to do it
@Doimaixinhtuoi
@Doimaixinhtuoi 3 года назад
In stead of initializing the blogs state with null value, is it ok to use an empty array?
@nenadbenke9834
@nenadbenke9834 Год назад
It keeps calling get request with useState. I just did it with fetch inside useEffect. By the way Shaun's way of teaching is the best for me.
@KehindeEnirayetan
@KehindeEnirayetan Год назад
Hello please I used the “empty array” approach and “null” approach but none worked. No blog was displayed on screen. Any hint with this
@PapsOSk19
@PapsOSk19 3 года назад
Great tutorial, thanks. I have just one question, how do you fetch it and map it inside the same component?
@akeymthedream
@akeymthedream Год назад
I think you can do so by using props to access blogs and then mapping the props in the BlogList component
@jonaskarl
@jonaskarl 3 года назад
Very good Playlist! Thanks. Is there a way, I can read and manipulate the blogs state in multiple components?
@LoveisHell85
@LoveisHell85 3 года назад
Amazing content. Thank you so much.
@teetphum
@teetphum 2 года назад
Thank you so much!! you save my web app
@Bashir3675
@Bashir3675 2 года назад
As you said it takes some time for the data to be fetched, that is why it is not available initially. But how using && is making the data available ? or is it taking some time until the the data gets fetched? Can somebody explain?
@satishganesan54
@satishganesan54 Год назад
Hi!!!... I am facing problem while fetching data , getting errors like err connection refused and am finding it hard to get solution in web and also I have faced issue in watching the json data after global installaton like running scripts is not allowed in this system..could you please reply?
@maxbisceglie1682
@maxbisceglie1682 3 года назад
Shouldn't the component update after blogs is updated without the conditional rendering? I thought the state variables were reactive... I'm a bit confused sorry
@shawnshaw9859
@shawnshaw9859 Год назад
first time when blogs was checked and it's null, what will happen, will it be rechecked regularly until it becomes non-null?
@bumpty9830
@bumpty9830 Год назад
I got CORS errors trying to follow this. You might think about adding a note, or maybe even a short tutorial video, about dealing with this. I had success after adding a --middlewares flag to the json-server run, pointing to a json file with the following contents: module.exports = (req, res, next) => { res.header('Access-Control-Allow-Origin','*'); next(); }
@GiancarloCarccamo
@GiancarloCarccamo 2 года назад
great video man
@mdkarim831
@mdkarim831 3 года назад
Thanks a bunch for these tutorials! Do you think you could show how to properly implement google OAuth 2.0 in a reactjs app? I have done an implementation but it looks very incorrect especially my redirect part.
@adventurer2395
@adventurer2395 2 года назад
How is res.json() async? Doesn't make sense, as it is already the result of an async response.
@oleksiik4473
@oleksiik4473 3 года назад
Hi, great job, thank you for your work! In this course, in a feature we see Redux?
@skipper3204
@skipper3204 Год назад
I'm wondering why would you use null as initial state of blogs. For me it works when initial state is empty array so it doesn't throw an error at the start. Any help?
@RahulSharma-ph2zn
@RahulSharma-ph2zn 2 года назад
after fetching api with useEffect when i reload the page on the browser. there is an error like, the initial state of the post is [ ] (a blank array). useEffect is call after the render. so the setPost can change the state later. and map function on a blank array is not possible.
@Se.mu01
@Se.mu01 3 года назад
Just what I needed
@alexhungerford
@alexhungerford 3 года назад
great summary at 6:39
@nerdiloo9863
@nerdiloo9863 3 года назад
"Maann, you are Metacmucil" 🔥🔥🔥
@jumbo999614
@jumbo999614 3 года назад
How come useQuery and usePaginatedQuery not included in this series? I know there is another tutorial series about useQuery. But the package is updated and the code taught in that series no longer work with new version.
@shehrozkhan7335
@shehrozkhan7335 2 года назад
{blogs && } i m still getting error msg
@alexandrudorultan9001
@alexandrudorultan9001 Год назад
Loading data/db.json Oops, data/db.json doesn't seem to exist Creating data/db.json with some default data when I tried to install json package can you help please to fix this issue thank you
@a.i.v7306
@a.i.v7306 3 года назад
How are you using react with regular .js files? I get errors on all my return code for my compnents when I try doing that. I have to have them be .jsx. Well Im using typescript so tsx
@CrookzArtwork
@CrookzArtwork 3 года назад
So clean
@SofianMW
@SofianMW 3 года назад
Perfect my friend!
@skia515
@skia515 10 дней назад
thanks for this video
@bartekk6480
@bartekk6480 2 года назад
thanks so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@themoroccanpianist8953
@themoroccanpianist8953 2 года назад
does this tutorial talk about the latest version of ReactJS (17) ?
@karolxbt8678
@karolxbt8678 8 месяцев назад
Starting from React 18, which is from 2022 onwards, it is not recommended to use useEffect for fetching data.
@mohsinmunir5267
@mohsinmunir5267 3 года назад
@thenetninja why you are using .then instead of await in this video for fetching data please explain i am watching this tutorial this is little bit confusing to me
@bananabread6758
@bananabread6758 Год назад
excellent video
@sohump7904
@sohump7904 3 года назад
Hi, I have the exact same code as you (I even tried downloading your GitHub repo) on my Mac, but I keep getting this error and I can't seem to solve the problem. Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Can you please help me fix this? I really want to progress on this course.
@ah_spider7350
@ah_spider7350 6 месяцев назад
6:35 in there what if I used tTernary operation?
@RHITMAN07
@RHITMAN07 3 месяца назад
The code didnt work because 'blogs' was not yet populated, i get this, as the call to get blogs is asynchronous...but how does adding ' blogs &&' solve the problem, how does this code ensure blogs is notl null by the time it is evaluated ???..this is the part i dont understand if anyone can explain ?? Thanks!!
@oybekmd
@oybekmd Месяц назад
const [blogs, setBlogs] = useState([]); //Remove null
Далее
Full React Tutorial #18 - Conditional Loading Message
3:47
Fetching Data in React - Complete Tutorial
29:10
Просмотров 124 тыс.
Как он понял?
00:13
Просмотров 108 тыс.
Full React Tutorial #16 - Using JSON Server
5:11
Просмотров 346 тыс.
4 Ways To Fetch Data in React
31:17
Просмотров 56 тыс.
Full React Tutorial #19 - Handling Fetch Errors
7:39
Просмотров 248 тыс.
How to FETCH data from an API using JavaScript ↩️
14:17
How to Fetch Data in React With A Custom useFetch Hook
16:37
Learn useEffect In 13 Minutes
13:38
Просмотров 837 тыс.
Every React Concept Explained in 12 Minutes
11:53
Просмотров 659 тыс.