0
I need to make a call to the API with React and for that I’m using Axios.
Doing a test with the code below, on the console I can see the result:
const [bill, setBill] = useState([]);
useEffect(() => {
api.post('/bills', { userId })
.then(response => console.log(response.data))
.catch(e => console.log(e));
console.log('bill', bill);
}, []);
But when I try to feed the state the code below, console.log
state does not change, remains empty.
const [bill, setBill] = useState([]);
useEffect(() => {
api.post('/bills', { userId })
.then(response => {
setBill(response.data);
})
.catch(e => console.log(e));
console.log('bill', bill);
}, []);