Reactjs/SemanticUi 在地图中添加 if 条件

Reactjs/SemanticUi add if condition in map

我想要的是在map函数中添加if条件,但没有成功:

const CityOptions = Data.CityData.map((state, index) => ({
    key: index,
    text: state.name,
    value: state.id,
}));

render(){
return(
<Dropdown
   id="City"
   search selection
   options={CityOptions}
></Dropdown>
}
}

我试过了:

 <Dropdown
     id="City"
     search selection
 >
     {Data.CityData.map((v,i) => {
         if(v.pid === this.state.cityValue){
             return (
                 <option key={i} value={v.id}>{v.name}</option>
             )
         }
         return true
     })}
 </Dropdown>

错误:

React.Children.only expected to receive a single React element child.

还有:

<Dropdown
   id="City"
   search selection
   options={CityOptions.pid === this.state.cityValue ? CityOptions : null}
/>

结果:

No data

你可以先filter

Data.CityData.filter(x => x.pid === this.state.cityValue).map((v,i)  =>{
    return <option key={i} value={v.id}>{v.name}</option>
})

根据 Semantic UI's docs optionsobjectsarray 遵循模式 { text: '', value: '' },因此您需要像

一样传递它]
const CityOptions = Data.CityData.filter(x => x.pid === this.state.cityValue).map((v,i)  =>{
    return{
        key: v.id,
        value: v.id,
        text: v.text
    }
})