TransWikia.com

why doesn't my React useState update my components?

Stack Overflow Asked by end-user on January 18, 2021

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.

2 Answers

Ok, yeah, it looks like the answer is to create separate state objects and update each accordingly; effectively what Garrett Motzner and tmohammad78 were saying. I think this lets the Component that are depending on them see the state change discretely. Would look like this:

const [countries, setCountries] = useState();
const [languages, setLanguages] = useState();

useEffect(() => {
    ...
    setLanguages(languages);
    ...
    setCountries(countries);
    ...
 }, [setLanguages, setCountries]);

If this were a larger Component, perhaps react-hooks-accessor-state would be a good approach.

Answered by end-user on January 18, 2021

i think you should separate the state if you don't want to set it together i tested it and I got error when set it separately , I think it's better separate it and write useEffect for each state if it's require

Answered by tmohammad78 on January 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP