I'm struggling with the following code and I don't get why I cant assign a value to searchResults :
const [searchResults, setSearchResults] = useState({});
const onSubmit = async () => {
try {
setIsLoading(true);
const response = await apiCall(formData);
console.log(response); // response is ok
setSearchResults(response);
console.log(searchResults); // searchResults is empty !!!
} catch (err) {
showNotification.error(err.message);
} finally {
setIsLoading(false);
}
};
Why searchResult is empty ?
I have checked some guides and examples :
React: Fetch Data onSubmit, not on onChange
and
https://www.codingdeft.com/posts/react-fetch-data-api/?utm_content=cmp-true
And I still don't understand why I cant assign a value to searchResults so I could render it after that.