skip to Main Content

I have 3 nested selects: Country -> State -> City.

But when I change the Country, the State is updated but not the City.

The code:
https://codesandbox.io/s/change-a-selects-options-based-on-another-dropdown-using-react-forked-hjfr2m?file=/src/App.js

2

Answers


  1. That is correct. When you change country, onChange in State

    <select onChange={handleChange}>{options}</select>
    

    gets triggered. So it changes the value. Since you are not triggering State, City is untouched

    Login or Signup to reply.
  2. You can add a useEffect in your Category component :

    useEffect(()=> {
      if (category) setSelected(category.options[0])
    }, [category])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search