[英]how to make select drop-down default select using react hooks
I have two select options one is dependent to other, One is country other is state so when I select Country I am changing state as well, which was working fine previously, but now as per my requirement I want to default select one country and show its cities which is also fine.我有两个选择选项,一个依赖于另一个,一个是国家,另一个是州,所以当我选择国家时,我也在更改州,以前工作正常,但现在根据我的要求,我想默认选择一个国家并显示它的城市也很好。
Issue is问题是
here in my code I Have made England as default selected, but when I am again selected other option suppose India, it is not selecting India.在我的代码中,我将英格兰设为默认选择,但是当我再次选择其他选项时,假设印度,它没有选择印度。
I don't know why this issue is showing up不知道为什么会出现这个问题
What I have done我做了什么
const [selected_country, setselected_country] = useState("England");
const [selected_state, setselected_state] = useState("London");
Creating states for default selected as above, after this在此之后为如上选择的默认创建状态
<select
name="Country"
value={selected_country}
onChange={(e) => onchange_device(e.target.value)}
>
{data.map((li, index) => (
<option key={index} value={li.country}>
{li.country}
</option>
))}
</select>
this is how I am looping through countries of my data这就是我遍历数据所在国家/地区的方式
<select name="city" value={selected_state}>
{cityOptions.map((city, index) => (
<option key={index} value={city.city_name}>
{city.city_name}
</option>
))}
</select>
the Above one is to set default cities.上面一个是设置默认城市。
I don't know where I am going wrong.我不知道我哪里出错了。
I made two changes,我做了两个改变,
const [cityOptions, setCityOptions] = useState(data[1].cities);
onChange
handler to the select dropdown in line 61.onChange
处理程序。<select name="city" value={selected_state} onChange={e => setselected_state(e.target.value)}>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.