I'm extremely new to React.js. I am trying to change a variable recepies which is in state using hooks. Below is my code snippet:
import React, { useState, useEffect } from 'react';
import './App.css';
function App() {
const [recepies, setRecepies] = useState([]);
useEffect(()=>{getRecepies()}, []);
//const example = Some valid API URL
const getRecepies = async () =>{
const response = await fetch(example);
const data = await response.json();
//data.hits is an array of 10 objects
setRecepies(data.hits);
console.log(recepies);
// prints nothing
}
return (
<div className="App">
</div>
);
}
export default App;
The problem is that setRecepies is NOT updating the recepies state. Why is that?data.hits is a valid array that has 10 objects that I'm fetching using an API.