3

I am trying to set the videos state to the items array of the response from the api but when I console.log the videos it returns an empty array.... Here is the code to fetch the api in which I set the videos state

const [videos, setVideos] = useState([]);
const handleFormSubmit = async (searchterm) => {
    await fetch(
      `https://www.googleapis.com/youtube/v3/search?${params.toString()}&q=${searchterm}`
    )
      .then((response) => response.json())
      .then((data) => {
        console.log(data);
        setVideos(data.items);
        console.log(videos);
      });
  };

How do I set the items array to the videos state?

3
  • 1
    setVideos is not a synchronous operation, the videos array will change the next time the functional component is rendered. Commented Aug 12, 2020 at 10:57
  • You can't log updated state right after calling setVideos. Your state is getting updated, your component will have to re-render to see the updated state. Also make sure you have a catch block to catch and handle any errors that might occur during the request. Commented Aug 12, 2020 at 11:00
  • Not related to your problem but either use async-await syntax or promise chaining, don't mix both of them. Commented Aug 12, 2020 at 11:08

3 Answers 3

7

Ciao, unfortunately with hooks you cannot log videos after setVideos call because setVideos is async.

To get the last value of videos you have to use useEffect hook in this way:

useEffect(() => {
   console.log(videos);
}, [videos]);
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah Exactly what I want to say
1

TLDR

React Hooks work async setState -> render -> state binding

Answer

react hooks is working like that

  1. setState
  2. schedule render component ( I mean state changed component)
  3. and render with changed state

So, You couldn't access video at that console.log(video)

Reference

  1. Overreact
  2. My medium with korean (You can translate with google)
  3. How does React Hooks re-renders a function Component?

Comments

0

You are using async/await, so it should be

const [videos, setVideos] = useState([])
const handleFormSubmit = async (searchterm) => {
  const response = await fetch(
    `https://www.googleapis.com/youtube/v3/search?${params.toString()}&q=${searchterm}`
  )
  const data = await response.json()
  console.log(data)
  setVideos(data.items)
  console.log(videos)
}

5 Comments

Nono, setVideos is async!!! Your solution it doesn't work at all!!!
Yes, I tries doing that but it still returns an empty array! :(
OP is trying to log the updated state right after calling setVideos which won't work. Using promise chaining inside an async function isn't the problem here.
@premkulkarni did you add the API key for the request?
The way I see it it doesn't matter cuz of it working same process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.