How To Make API Call In React using useEffect() Hook With Fetch API and async / await

Поділитися
Вставка
  • Опубліковано 17 гру 2024

КОМЕНТАРІ •

  • @AleySoundz
    @AleySoundz Рік тому +7

    watched videos that were hours long and this three minute video is what actually made everything click. Thank you.

  • @alanbalvin7731
    @alanbalvin7731 Місяць тому +1

    Beautiful, straight forward to the point!!!

    • @ghosttogether
      @ghosttogether  Місяць тому +1

      @@alanbalvin7731 thanks glad it worked out for you :)

  • @mobileeats-q2i
    @mobileeats-q2i Рік тому +6

    On line 12 at @2:50, why did you use ".then" on result.json() if you were already using async/await syntax for the fetch line above it? Can we not just use "const data = await result.json()" ?

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

      Can we? Await is designed to return a promise, not the data itself.

    • @zortric
      @zortric 9 місяців тому +1

      @vuestacks @ghosttogether No, the Response.json method returns a promise, but by using await it waits for the promise to resolve before assigning the value. That is the whole point.
      "const data = result.json()" sets data to the promise object
      "const data = await result.json()" sets data to what the promise resolves to, aka the response converted to json.

    • @АртурВолков-в2ъ
      @АртурВолков-в2ъ 8 місяців тому

      @@ghosttogether No need in .then
      useEffect(() => {
      const fetchData = async () => {
      const data = await fetch(URL);
      const {current, location} = await data.json();

      setTemp(current.temp_f)
      }

      fetchData();
      }, []);

    • @abhiwastaken
      @abhiwastaken 4 місяці тому

      Actually both of the methods do the same thing, await is just fancy .then() underneath

  • @tiborifj.fazekas6251
    @tiborifj.fazekas6251 Рік тому +2

    Simple concept but super helpful❤

  • @Dianagarciax
    @Dianagarciax Рік тому +3

    This was super helpful! Thank you

  • @eddysantiagorhenalsperez1139

    Thanks this is very useful, what can I do if I want the temperature to change when I click a button?

  • @jackepner9984
    @jackepner9984 Рік тому +4

    Why would you put your API key on the front-end!? Nobody builds API calls this way...

    • @Romeslayer
      @Romeslayer Рік тому +8

      For demonstration purposes

    • @Acceleratedpayloads
      @Acceleratedpayloads 9 місяців тому

      Yikes, I guess you still need something like a csrf token even if you move to backend

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

    Why don't we use fetch directly but we created a separate function?

  • @solver55
    @solver55 5 місяців тому

    Where can i get the code repo.

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

    Thank you very much!

  • @spamcolector
    @spamcolector 5 місяців тому

    as a guy who never worked with api and react. that code is discountingly hard to read.

  • @AB-kq9xm
    @AB-kq9xm Рік тому

    Does this still work? Copied this line for line and get - cannot read properties of undefined (reading 'temp_f'). Seems like you need a special key (not the standard one.