响应选择器的本机值

时间:2018-10-16 09:37:29

标签: react-native firebase-realtime-database picker

doc:https://facebook.github.io/react-native/docs/picker

构造函数:

constructor(props) {        
  super(props);         
  this.state = {            
      showPass: true,
      press: false,
      email: "",
      age: 0,
      sexe:"",
      password: "",
      confirmPass: ""       
     } 
  }

JSX:

<Picker
    selectedValue={this.state.sexe}
    onValueChange={(value) => {this.setState({sexe: value})}}>
    <Picker.Item label="Homme" value="homme" />
    <Picker.Item label="Femme" value="femme" />
</Picker>

问题:我不知道为什么this.state.sexe不受影响

Database user PS:React Native的初学者

1 个答案:

答案 0 :(得分:1)

您应该在状态上指定默认选择,然后只有onValueChange才能触发

尝试以下代码

  constructor(props) {        
  super(props);         
  this.state = {            
      showPass: true,
      press: false,
      email: "",
      age: 0,
      sexe:"homme",
      password: "",
      confirmPass: ""       
     } 
  }

和JSX

<Picker
    selectedValue={this.state.sexe}
    onValueChange={(value) => {this.setState({sexe: value})}}>
    <Picker.Item label="Homme" value="homme" />
    <Picker.Item label="Femme" value="femme" />
</Picker>