score:1

Accepted answer
useEffect(() => {
  const consultarAPI = async () => {
    const API = '81673cba4b934873953af1db871a6ac7';
    const url = `https://newsapi.org/v2/top-headlines?country=br&category=${categoria}&apiKey=${API}`;

    try {
      const resposta = await fetch(url);
      if (!resposta.ok) {
        throw new Error('Network response was not ok');
      }
      const noticias = await resposta.json();
      guardarNoticias(noticias.articles);
    } catch(error) {
      // handle fetch failure or any other thrown errors
    }
  }
  consultarAPI();
}, [categoria]);

Related Query

More Query from same tag