【发布时间】:2020-12-24 17:22:31
【问题描述】:
我在我的项目中使用react-native-dropdown-picker。我有一个状态数组,如下所示:[{value: "Bagerhat"},{value: "Bagerhat"}]。因此,在 ComponentDidMount 中,我将这个数组更改为[{label: "Bagerhat"},{label: "Bagerhat"}],因为 react-native-dropdown-picker 需要在它的项目数组中添加标签。这是我的DropDownPicker 组件:
componentDidMount() {
var sts = [{value: "Bagerhat"},{value: "Bagerhat"}]
sts = sts.map(item => {
return {
label: item.value
};
});
this.setState({
states: sts
})
}
<DropDownPicker
items={this.state.states}
defaultValue={(this.state.state !== ''? this.state.state : null )}
containerStyle={{ height: 50,width: width - 40,alignSelf:'center' }}
style={{backgroundColor: '#fff'}}
itemStyle={{justifyContent: 'flex-start'}}
dropDownStyle={{ marginTop: 2, backgroundColor: '#fff' }}
onChangeItem={item => this.setState({state:item.label})}
/>
现在this.state.state 像“Bagerhat”一样出现。但它显示label 的错误未定义。为什么会抛出这个错误。如果我删除默认值但默认值由于某种原因抛出标签错误,它会在下拉列表中显示状态。这是为什么呢?
【问题讨论】:
-
你的情况是什么
this.state.state? -
它只是像“Bagerhat”这样的字符串@LeriGogsadze
-
我认为应该是键值对。
{ label: "Bagerhat" } -
也试过了。没用。