简体   繁体   English

React Native - 连接两个选择器

[英]React Native - Connect two pickers

How I can connected two pickers in react native.我如何在 React Native 中连接两个选择器。

For example user has first_name and last_name.例如用户有 first_name 和 last_name。 When the user click on first picker and select first_name automatically change the value in second picker last_name.当用户单击第一个选择器并选择 first_name 时,会自动更改第二个选择器 last_name 中的值。

For example we have user with first_name = 'John' and last_name = 'Deep'.例如,我们有 first_name = 'John' 和 last_name = 'Deep' 的用户。 When the user click on first picker John automatically in second picker set value to Deep.当用户在第二个选择器中自动单击第一个选择器 John 时,将值设置为 Deep。

I don't use library for picker, but I use library for local database: https://www.npmjs.com/package/react-native-sqlite-storage我不为选择器使用库,但我为本地数据库使用库: https : //www.npmjs.com/package/react-native-sqlite-storage

you can call a function with onValueChange everytime picker changed每次选择器更改时,您都可以使用onValueChange调用函数

<Picker
   selectedValue={this.state.firsname}
   style={{height: 50, width: 100}}
   onValueChange={(itemValue, itemIndex) =>
    this.yourfunc(itemValue, itemIndex)
   }>
 <Picker.Item label="John" value="John" />
 <Picker.Item label="abc" value="abc" />
</Picker>

<Picker
   selectedValue={this.state.lastname}
   style={{height: 50, width: 100}}>
 <Picker.Item label="Deep" value="Deep" />
</Picker>

//set your selectedValue to the state where you save the value

....

yourfunc = (val, index) => {
   //doing some stuff to change another picker
   let lastname = //get lastname from val
   //and then this.setState lastname
}

Official docs官方文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM