I have a picker that I'm testing on iOS right now with two options. Every time I drag down from the first option to the second option, the picker immediately returns to the first option.

我有一个我正在iOS上测试的选择器,有两种选择。每次我从第一个选项向下拖动到第二个选项时,选择器会立即返回第一个选项。

This is what my the code for my picker looks like.

这就是我的选择器代码的样子。

<Picker 
      style={{
        width: 100,
      }}
      selectedValue={(this.state && this.state.pickerValue) || 'a'}
      onValueChange={(value) => {
        this.setState({value});
      }} itemStyle={{color: 'white'}}>
      <Picker.Item label={'Hello'} value={'a'} />
      <Picker.Item label={'World'} value={'b'} />
</Picker>

I want the selector to stay at the newly scrolled-to option. I've also removed the || 'a' part of the selectedValue attribute but that didn't solve the issue either.

我希望选择器保持在新滚动的选项中。我也删除了|| selectedTalue属性的'a'部分,但也没有解决问题。

2 个解决方案

#1


17

On value change you need to specify which property of the state changed and change it accordingly with this.setState

在值更改时,您需要指定状态的哪个属性已更改,并使用this.setState相应地更改它

onValueChange={(value) => {this.setState({pickerValue: value});

Complete Code

<Picker 
      style={{
        width: 100,
      }}
      selectedValue={(this.state && this.state.pickerValue) || 'a'}
      onValueChange={(value) => {
        this.setState({pickerValue: value});
      }} itemStyle={{color: 'white'}}>
      <Picker.Item label={'Hello'} value={'a'} />
      <Picker.Item label={'World'} value={'b'} />
</Picker>

更多相关文章

  1. JavaScript 对象属性作实参以及实参对象的callee属性
  2. 如何为select中的选项创建?
  3. 使用jackson json将属性添加到json字符串
  4. React组件的属性PropTypes
  5. Javascript XML DOM将属性设置为特定元素
  6. 为什么我收到此错误:“未捕获的TypeError:无法读取未定义的属性'标
  7. 打字稿:无法访问类属性[重复]
  8. 创建一个对外界只读的属性,但是我的方法仍然可以设置
  9. Javascript没有返回样式属性[重复]

随机推荐

  1. golang中协程和线程的区别是什么?
  2. golang编程语言能开发啥
  3. golang数据类型有哪些
  4. golang数组与切片的区别是什么?
  5. golang语言可以做些什么
  6. golang如何判断数据类型
  7. golang是什么语言?
  8. golang语言的出现解决了什么问题
  9. golang如何创建map
  10. golang有什么用途?