简体   繁体   English

在“react-native-dropdown-picker”中设置默认值

[英]Set default value in 'react-native-dropdown-picker'

I'm using react-native-dropdown-picker in my project.我在我的项目中使用react-native-dropdown-picker I have an array of states which is like this: [{value: "Bagerhat"},{value: "Bagerhat"}] .我有一个状态数组,如下所示: [{value: "Bagerhat"},{value: "Bagerhat"}] So, in ComponentDidMount i'm changing this array by [{label: "Bagerhat"},{label: "Bagerhat"}] because react-native-dropdown-picker needs label in it's item's array.因此,在 ComponentDidMount 中,我通过[{label: "Bagerhat"},{label: "Bagerhat"}]更改此数组,因为 react-native-dropdown-picker 需要 label 在它的项目数组中。 Here's my DropDownPicker component:这是我的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})} />

Now this.state.state is present like 'Bagerhat' for example.现在this.state.state像“Bagerhat”一样出现。 But it's showing error for label is undefined.但它显示label的错误未定义。 Why it's throwing this error.为什么会抛出这个错误。 On dropdown it's showing states if i remove defaultvalue but defaultValue is throwing label error for some reason.如果我删除默认值但默认值由于某种原因抛出 label 错误,它会在下拉列表中显示状态。 Why is that?这是为什么?

The reason it is crashing is it can't take null as a default value, You should remove ternary if else in default value and set this.state.state in constructor as an example below,它崩溃的原因是它不能将 null 作为默认值,您应该在默认值中删除三元 if else 并设置 this.state.state 作为下面的构造函数示例,

export default class Picker extends Component {
   constructor(){
     this.state={
        state:'',
     }
  }
} 

And in return function, change that to作为回报 function,将其更改为

<DropDownPicker
      items={this.state.states}
      defaultValue={(this.state.state)}
      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})}
    />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法将下拉选择器中每个条目的 numberOfLines 设置为 1? (react-native-dropdown-picker) - Is there a way to set numberOfLines to 1 of each entry in the dropdown picker? (react-native-dropdown-picker) react-native-dropdown-picker 按条件更改下拉背景颜色 - react-native-dropdown-picker changing dropdown background color by condition react-native-dropdown-picker 列出的项目未正确显示(覆盖) - react-native-dropdown-picker listed items not showing properly (Overlay) 如何在 react-native-dropdown-picker 中调整模型大小 - How to resize Model in react-native-dropdown-picker react-native-dropdown-picker,如何修复下拉选择器覆盖在其他组件上 - react-native-dropdown-picker, how to fix the dropdown picker overlay on other component 有没有办法降低下拉选择器的高度(react-native-dropdown-picker 模块) - Is there a way to decrease the height of dropdown picker (react-native-dropdown-picker module) react-native-dropdown-picker,如何从项目中获取选定的索引 - react-native-dropdown-picker, how to get selected index from Items React-native 选择器默认值 - React-native picker default value React-Native Picker 无法初始化默认值 - React-Native Picker cannot initialize a default value 如何在 React Native 中为 props 设置默认值 - How to set a default value for props in React native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM