I'm sure this is just my lack of React understanding, but I'm trying to get a Component to update after the state is updated (which is happening asynchronously).
const [appState, setAppState] = useState({
countries: null,
languages: null
})
useEffect(() => {
const languageURL = `//localhost:8080/languages`,
countryURL = '//localhost:8080/countries';
axios.get(languageURL, {
headers: {'Access-Control-Allow-Origin': '*'}
})
.then((langResponse) => {
const languages = langResponse.data.map(l => ({
value: l.id,
label: l.language + " (" + l.code + ")"
}))
setAppState({languages: languages});
console.log("updating languages");
return languages;
}, (error) => {
console.log(error)
}
);
axios.get(countryURL, {
headers: {'Access-Control-Allow-Origin': '*'}
})
.then((countryResponse) => {
const countries = countryResponse.data.map(l => ({
value: l.id,
label: l.name + " (" + l.code + ")"
}))
setAppState({countries: countries});
console.log("updating countries");
return countries;
}, (error) => {
console.log(error)
}
);
}, [setAppState]);
My Component looks like this (I have one for each object set):
<Select
options={appState.countries}
isSearchable
/>
I've verified that the data is coming down and that it matches the format. The problem appears to be that each Component is not rendering when the supplied state object updates - only one is picking up changes. It also seems like it's whichever resolves first - the other one is empty. It's almost the opposite of this question - they are updating immediately, before the other set has been resolved.