4 Ways To Fetch Data in React

Поділитися
Вставка
  • Опубліковано 20 лип 2024
  • In this video I will go over 4 ways you can fetch data in react from most basic to more advanced. I will also introduce the topic of best practices while fetching data.
    Code: github.com/machadop1407/react...
    Join our Discord: / discord
    🚀 Learn ReactJS By Building 6 Projects: codedamn.com/learn/reactjs-pr...
    🐙 GraphQL Course: codedamn.com/learn/graphql-fo...
    ► Buy Crypto on Coinbase: coinbase-consumer.sjv.io/PedroTech
    Social
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    Website: machadopedro.com
    Linkedin: / machadop1407
    Instagram: / _pedro.machado_
    Github: github.com/machadop1407
    Business Email: pedro@pedrotech.co
    Tags:
    - ReactJS Tutorial
    - ReactJS and MySQL
    - NodeJS Tutorial
    - API Tutorial
    * React Fetch Data*
    00:00 | Intro
    00:52 | Fetch API
    03:43 | Axios
    06:27 | useSWR
    14:30 | React Query Library
    #reactjs #data
  • Наука та технологія

КОМЕНТАРІ • 93

  • @behold_a_son
    @behold_a_son 2 роки тому +20

    2:49 The result is not JSON, here's a quote from mdn on Response.json():
    "Note that despite the method being named json(), the result is not JSON but is instead the result of taking JSON as input and parsing it to produce a JavaScript object."
    So correct me if I am wrong: the result obtained after the fetch promise has fulfilled, is already in JSON. When you do res.json(), it returns another promise that converts JSON into a JavaScript Object, so you can for example use the dot operator on the data when the returned promise of res.json() is fulfilled.
    We all are learning! Don't let a mistake like that haunt you Pedro! You've been a great help to me! :)

    • @PedroTechnologies
      @PedroTechnologies  2 роки тому +9

      Yes! The way i said it came out wrong, but you are right 🙂 The function parses a json into a js object.

  • @madalinavasile5930
    @madalinavasile5930 2 роки тому +4

    Hi! I just want to say a big thank you! For my graduation project/bachelor thesis I decided to make an app using React. It was a totally new technology for me and the first app I ever made. Finding your tutorials and videos was been really helpful, they're really easy to follow and your explications and examples are great! I'll have to make sure to go back to every video I watched and leave a like. Good job and hopefully more people will find your channel!

    • @PedroTechnologies
      @PedroTechnologies  2 роки тому +1

      This makes me really happy! Im glad you were able to learn from my videos 🙂

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

    I heard and use some basics of React Query but you mentioned many things that I did not know and they are very useful! Thank you! :)

  • @universecode1101
    @universecode1101 2 роки тому +2

    This is a very important step guys … good video Pedro 👏🏻

  • @Moon-li9ki
    @Moon-li9ki 2 роки тому +1

    boa pedrão, teus videos sempre me ajuda demais! Nem sabia q vc era brasileiro, seu inglês é muito bom

  • @fandross_
    @fandross_ 2 роки тому +2

    E ai mano, tu eh o melhor professor que eu ja tive, valeu ai!

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

    Thank you so much, Pedro, for the video. I have been watching many videos, but yours are by far more helpful. Would you be able to do a tutorial on Nivo charts API integration? This topic is poorly documented and it would help tremendously to have your guidance. I am pretty sure I am not the only one searching for it 😅. Many thanks 😊

  • @arsnakehert
    @arsnakehert 2 роки тому

    Your channel is a treasure of knowledge, man

  • @michealdalu8620
    @michealdalu8620 2 роки тому +1

    This is gold .. thanks 🙌🏽👏🏾👏🏾👏🏾

  • @sethheinzman5823
    @sethheinzman5823 2 роки тому

    You can also use SWR config to set a fetcher and set the suspense for all of the useSWR so you can just include the url and nothing else. That’s how this book taught it but I kind of like having the option to change the fetcher.

  • @WTFBRUTUS
    @WTFBRUTUS 2 роки тому +1

    Just what I needed to know thanks!!

  • @nomapos
    @nomapos 2 роки тому +5

    Hello, please we need some react native content ....
    Even if it's only notes ...
    Please share ...🙌🙌

  • @himanshugaikwad4998
    @himanshugaikwad4998 2 роки тому

    Nice video like always. Thank you very much. Can u make a video on auto image slider in react js? i would love to learn that from u

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

    Thank you , very useful video ❤

  • @varaprasad6531
    @varaprasad6531 2 роки тому

    Thanks Pedro!

  • @licokr
    @licokr 2 роки тому

    Thank you so much! Great!

  • @DeepLook1
    @DeepLook1 2 роки тому

    Thank uuuuuuuu . And how many ways we have to insert record to database like mysql. Thank u again for this video

  • @Hacking-NASSA-with-HTML
    @Hacking-NASSA-with-HTML Рік тому

    Thank you so much, sir 👍♥️🤝

  • @tech3425
    @tech3425 2 роки тому

    Cool vid. Would love to see something like using react query with firebase, if that's something you find interesting.

  • @EduardKaresli
    @EduardKaresli 2 роки тому +1

    There is also RTK Query, from Redux Toolkit.

  • @sonamohialdin3376
    @sonamohialdin3376 2 роки тому

    Awesome tutorial

  • @sujoykrhaldar
    @sujoykrhaldar 2 роки тому

    hi
    Can you make a series on how to use swr client side fetching in nextjs and react query as well.

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

    great video, thx

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

    love it!

  • @atandauthman2126
    @atandauthman2126 2 роки тому

    I can see you have the copilot extension, I'm sort of having issues getting my own to work I don't know what I'm doing wrong but it shows the icon as cancelled every time

  • @jorge.camargo
    @jorge.camargo Рік тому

    Fantastic. Thank you!

  • @diegounanue
    @diegounanue 2 роки тому +4

    More videos about how to structure a large project with react query :)

  • @adarsh-chakraborty
    @adarsh-chakraborty 2 роки тому

    I wish you showed stale option in react-Query. Anyways nice video!!

  • @AM-nm6ts
    @AM-nm6ts 2 роки тому +1

    please if it's possible, when u have time, ---> context api vs prop drilling thanks a lot

  • @Ellenox147
    @Ellenox147 2 роки тому

    Great video thanks ! What's the theme plzzzzzzz ?

  • @filipevalentegomes2383
    @filipevalentegomes2383 2 роки тому +2

    I’m surprised you didn’t mention rtk query, that’s another one, lots of people love using. Still great video

    • @PedroTechnologies
      @PedroTechnologies  2 роки тому +1

      Rtk query is good! I didn't mention because I don't use it that much (very few times) so I can't explain it to the best of my habilities

    • @rohan1765
      @rohan1765 2 роки тому

      RTK query is so cool 🙂

  • @GabrielMartinez-ez9ue
    @GabrielMartinez-ez9ue 2 роки тому +1

    Thank you dude. Is the suspense new feature still experimental or can it be used in production for nextjs apps?

    • @PedroTechnologies
      @PedroTechnologies  2 роки тому +1

      Its not experimental anymore, it came with react 18!

  • @jackiedo7370
    @jackiedo7370 2 роки тому +1

    just cmt to say thank you ❤

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

    Could you please help me how to fetch data from mssql database stored procedure to reactjs

  • @kabagambedaniel2468
    @kabagambedaniel2468 2 роки тому +1

    nice one

  • @sudiptokumarmitra9845
    @sudiptokumarmitra9845 2 роки тому +2

    Can you please make a project-based tutorial using the redux toolkit and react query and show how to create industry standard coding and folder structure??

    • @songokussj4cz
      @songokussj4cz 2 роки тому

      +1 ! Show us something real, please. Best practices in your opinion.

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

    Thanks!

  • @patrickorji3616
    @patrickorji3616 2 роки тому

    I don't have anything displaying on my browser when I use the Fetch API and Axios API. All my codes are correct just like that of Pedro's. I will appreciate a response

  • @icecream5335
    @icecream5335 2 роки тому

    I want to fetch the data from the server which uses jwt auth. Please help me.

  • @shinobi_coder88
    @shinobi_coder88 2 роки тому +1

    Hey Pedro, just wanna clarify that in React 18 anytime when there's sth to do with api we no longer need to use useEffect() right?
    Since React Query benefits our performance better. Honestly I've tried both Pagination and Infinite Scroll in React Query which are really good in React app. However, when I use React Query in NextJs that's where I feel it slows down the app performance a bit.

    • @PedroTechnologies
      @PedroTechnologies  2 роки тому

      I mean, it depends on how your app is running. I usually use SWR when im working in a nextjs app and react query when I am not. But both should be good, try to optimize the way your are fetching ur data and see if it is react query that is really causing the issue. For example, are you paginating by fetching data incrementally?

    • @shinobi_coder88
      @shinobi_coder88 2 роки тому

      @@PedroTechnologies what do you mean by incrementally?
      In my scenario, for example Rick and Morty API, when it comes to NextJs the only thing that React Query helps is prefetching data with SSR or SSG. But when clicking to the next new page (which is not the previous data yet) then it renders not as fast as React App.
      On the other hand, I have also tried to fetch Weather API with React Query in NextJs and it seems quite slow too compared to using only SSR to fetch the data from Weather API

    • @shinobi_coder88
      @shinobi_coder88 2 роки тому

      @@PedroTechnologies I'm also curious what makes you choose SWR instead of React Query for NextJs App ?

    • @ramonarthur2729
      @ramonarthur2729 2 роки тому

      @@shinobi_coder88 export const getStaticProps: GetStaticProps = async (context) => {
      const { slug } = context.params as IParams;
      const queryClient = new QueryClient();
      await queryClient.prefetchQuery(['product', slug], () => fetchProduct(slug));
      await queryClient.prefetchQuery('products', fetchProducts);
      return {
      props: {
      dehydratedState: dehydrate(queryClient),
      },
      };
      };

    • @ramonarthur2729
      @ramonarthur2729 2 роки тому

      @@shinobi_coder88 export default function ProductDetails() {
      const router = useRouter();
      const { slug } = router.query as IParams;
      const { data: product } = useQuery(['product', slug], () =>
      fetchProduct(slug)
      );
      const { data: products } = useQuery('products', fetchProducts);
      if (!product) return ;
      .....

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

    18 version is fast but 16 version too slow my office project running 16 version how to fast do it?

  • @FaisalAmin001
    @FaisalAmin001 2 роки тому +2

    Please also make video on testing i.e Jest etc
    Thanks

    • @PedroTechnologies
      @PedroTechnologies  2 роки тому +1

      Just posted one a couple hours ago hahaha

    • @FaisalAmin001
      @FaisalAmin001 2 роки тому

      @@PedroTechnologies wow, hahahah
      thanks bro ❤

  • @smartjefreycoca5425
    @smartjefreycoca5425 2 роки тому

    Brother pedro can you make a tuotrial creating a User Management System using React Js and MUI

  • @mentoriii3475
    @mentoriii3475 2 роки тому +1

    1:55 fetch api is a browser api, technically not built in into javascript but in the browser

    • @PedroTechnologies
      @PedroTechnologies  2 роки тому

      Yes you are right! In the video I meant to say that u can use i without installing any library (such as axios). I see how it sounds wrong now hahaha

  • @renesalvacion6169
    @renesalvacion6169 2 роки тому +1

    Do I need to fetch data in react, if I used nodejs for backend?

    • @adarsh-chakraborty
      @adarsh-chakraborty 2 роки тому +1

      Yes frontend needs to do network requests to get data from backend. Doesn't matter if it's node server, python or java.

    • @PedroTechnologies
      @PedroTechnologies  2 роки тому

      The frontend has to fetch the data from the backend in order to get the data.

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

    ainda bem que vc não usou o typescript

  • @aghilannathan8169
    @aghilannathan8169 2 роки тому

    Damn to Redux Toolkit Query.

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

    Hi, am using axios to fetch data but am getting this kind of error...... AxiosError: Network Error

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

      There might be an error with your network.

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

      @@fullfungo I have come a long way😅, at that time such an error gave me headaches now am good at react.js and react native🤣
      Thank you for the response anyway.

  • @ts8960
    @ts8960 2 роки тому

    async await is the best way.

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

    am dead 29:29 "writting code that doesn't male sense"

  • @abulazaiemabas5070
    @abulazaiemabas5070 2 роки тому

    Fetch api is built-in the browser not node

  • @mr-36
    @mr-36 2 роки тому

    Fetch is not part of javascript. Fetch belongs to BOM(browser object model)

    • @nanimys
      @nanimys 2 роки тому

      in turn, belongs to the javascript library!!

    • @PedroTechnologies
      @PedroTechnologies  2 роки тому

      I meant that when you are using javascript, you don’t have to download any extra library to use the fetch api.

    • @mentoriii3475
      @mentoriii3475 2 роки тому

      @@nanimys no sir, there is a reason it's called an api, we use it on javascript but it's not built in javascript

    • @mr-36
      @mr-36 2 роки тому

      @@mentoriii3475 Exactly!

  • @SahraClayton
    @SahraClayton 2 роки тому +1

    React Query is by no means a state solution, also I like your videos but just get to the point, it took you over 4 minutes from the start of the React Query part to even get to the point of React Query

  • @RicardoGuillen
    @RicardoGuillen 2 роки тому +1

    You forgot RTK Query :/

    • @PedroTechnologies
      @PedroTechnologies  2 роки тому +1

      RTK Query is interesting, but I don't use it a lot since I am not that fond about Redux

    • @RicardoGuillen
      @RicardoGuillen 2 роки тому +1

      what do you recommend to use with Zustand?
      Zustand + React Query?

    • @GabrielMartinez-ez9ue
      @GabrielMartinez-ez9ue 2 роки тому

      @@RicardoGuillen if you know RTK, stick to that one and learn it well. Its great

  • @aravindsanjeev4150
    @aravindsanjeev4150 2 роки тому

    Who wants to steal his intro bgm?

  • @Pareshbpatel
    @Pareshbpatel 2 роки тому +1

    Great Tutorial on four ways to fetch data in React. Thanks, Pedro
    {2022-06-14}, {2023-09-06}

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

    const { isLoading, error, data, isFetching } = useQuery(["dog"], () =>
    axios
    .get("random.dog/woof.json")
    .then((res) => res.data)
    );
    This is right way