Reactjs 如何在react钩子中更改其他选项时更新一个选择选项

Reactjs 如何在react钩子中更改其他选项时更新一个选择选项,reactjs,react-hooks,Reactjs,React Hooks,我有一组数据,我想通过这些数据更新我的UIselect选项,但我不知道如何才能做到这一点 JSON数据 [ { country: 'India', cities: [ { city_name: 'New delhi', }, { city_name: 'mumbai', }, ], }, { country: 'England', cities:

我有一组数据,我想通过这些数据更新我的UI
select选项
,但我不知道如何才能做到这一点

JSON数据

[
{
    country: 'India',
    cities: [
        {
            city_name: 'New delhi',
        },
        {
            city_name: 'mumbai',
        },
    ],
},
{
    country: 'England',
    cities: [
        {
            city_name: 'London',
        },
        {
            city_name: 'manchester',
        },
    ],
},
{
    country: 'Portugal',
    cities: [
        {
            city_name: 'Lisbon',
        },
    ],
},
];
我试过的是:

                   <select
                        name="country">
                        {data.map((li, index) => (
                                <option
                                    key={index}
                                    value={li.country}
                                    onchange={() => onchange_device(li.country)}>
                                    {li.country}
                                </option>
                            ))}
                    </select>


                 <select
                                name="city">
                                <option
                                    value="city val
                                    >
                                    here I want to show city
                                </option>
                            ))}
                    </select>

{data.map((li,index)=>(
onchange_设备(li.country)}>
{li.country}
))}

您需要将
onchange
重构为
onchange
。您需要将此道具分配给
选择
元素,而不是
选项
元素

<select name="Country" onChange={(e) => onchange_device(e.target.value)}>
然后在值之间映射

<div className="App">
  <select name="Country" onChange={(e) => onchange_device(e.target.value)}>
    {data.map((li, index) => (
      <option key={index} value={li.country}>
        {li.country}
      </option>
    ))}
  </select>
  <select name="city">
    {cityOptions.map((city, index) => (
      <option key={index} value={city.city_name}>
        {city.city_name}
      </option>
    ))}
  </select>
</div>

onchange_设备(例如target.value)}>
{data.map((li,index)=>(
{li.country}
))}
{cityOptions.map((城市,索引)=>(
{城市。城市名称}
))}

<div className="App">
  <select name="Country" onChange={(e) => onchange_device(e.target.value)}>
    {data.map((li, index) => (
      <option key={index} value={li.country}>
        {li.country}
      </option>
    ))}
  </select>
  <select name="city">
    {cityOptions.map((city, index) => (
      <option key={index} value={city.city_name}>
        {city.city_name}
      </option>
    ))}
  </select>
</div>