I have function that gets the data from API request. I want to put this data to the state variable, but for some kind of reason it doesn’t work, when printing the state it always shows the empty array.
export default function Buttons({setOptionButtons, sendMessage, places}){
const [data, setData] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
const [names, setNames] = useState([])
const [addresses, setAddresses] = useState([])
useEffect(()=>{
console.log(data)
}
,[data])
const pressOption = (input, option) => {
const fetchData = async () => {
for (const place of option){
setLoading(true);
try {
const requests = option.map((place) =>
axios.get(`url`)
);
const responses = await Promise.all(requests);
console.log(responses)
const items = responses.flatMap((response) => response.data.result.items);
const newNames = items.map((item) => item.name);
const newAddresses = items.map((item) => item.address_name);
setNames((prevNames) => [...prevNames, ...newNames]);
setAddresses((prevAddresses) => [...prevAddresses, ...newAddresses]);
setError(null);
console.log('Hello');
console.log(names)
} catch(error) {
setError(error.message)
} finally {
setLoading(false)
}
}
};
fetchData();
setOptionButtons(false);
sendMessage(input);
}
return (
///html code
)
}
I tried to use promise.all and to call the printing outside the fetchData function. It should print data which I pass via useState.
2
Answers
It seems like the problem was the line
setOptionButtons(false)
, which is asyncrhonous. So I addedawait
to this line and shuffled it withupdateOptionButtons()
, so that it waits until first asynchronous function ends.Also, it is better to use
Promise.all
to handle multiple requests to API as I did here instead of using for-loop.I have taken the liberty of refactoring your code to the point where it should work, the issue I have discovered is that call to
Array.map
inside the for loop, this seems to bug out your state. You will find comments I have added across your function explaining why I have done what,Also you seem to be new to react, definitely take a look at the official documentation for react. They have just updated their entire documentation and it is really informative.
PS:
As the comments state above you will have pretty excessive requests to the API your calling, if you are paying per request or bandwidth I strongly recommend another refractor over that passage of code.