简体   繁体   English

如何根据选择器的当前值使用 react-native-picker 在页面上显示内容?

[英]How can I use react-native-picker to display content on the page depending on the current value of the picker?

I am trying to use react-native-picker in my project and recently came across this Expo Snack: https://snack.expo.dev/HkM_BcGBW我正在尝试在我的项目中使用react-native-picker ,最近遇到了这个 Expo Snack: https ://snack.expo.dev/HkM_BcGBW

I want to display content on the page depending on the current value of the picker.我想根据选择器的当前值在页面上显示内容。 For instance, some text right below the picker saying "The option you have selected is [text of the currently-selected option in the picker]."例如,选择器正下方的一些文本说“您选择的选项是[选择器中当前选择的选项的文本]”。 How can I do this?我怎样才能做到这一点?

You can do as the example and use state value to display in the component您可以作为示例并使用状态值在组件中显示

import React, { Component } from 'react';
import { Text, View, StyleSheet, Picker } from 'react-native';

export default class App extends Component {
  constructor(props) {
    super(props)
    
    this.state = {
      language: 'java',
    }
  }
  
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.title}>Unstyled:</Text>
        <Picker
          style={styles.picker} itemStyle={styles.pickerItem}
          selectedValue={this.state.language}
          onValueChange={(itemValue) => this.setState({language: itemValue})}
        >
          <Picker.Item label="Java" value="java" />
          <Picker.Item label="JavaScript" value="js" />
          <Picker.Item label="Python" value="python" />
          <Picker.Item label="Haxe" value="haxe" />
        </Picker>
        
        <Text>The option you have selected is {this.state.language}</Text>
      </View>
    );
  }
}

But do remember但请记住

onValueChange={(itemValue) => this.setState({language: itemValue})}

this stores value rather than the label.这存储值而不是标签。

you can use conditional rendering as below:您可以使用如下条件渲染:

import React, { Component } from 'react';
import { Text, View, StyleSheet, Picker } from 'react-native';

export default class App extends Component {
   constructor(props) {
     super(props)

     this.state = {
       language: 'haxe',
       firstLanguage: 'java',
       secondLanguage: 'js',
     }
   }

   render() {
     return (
       <View style={styles.container}>
         <Text style={styles.title}>{this.state.language}</Text>
         <Picker
           style={styles.picker} itemStyle={styles.pickerItem}
           selectedValue={this.state.language}
           onValueChange={(text) => this.setState({language: text})}
         >
           <Picker.Item label="Java" value="java" />
           <Picker.Item label="JavaScript" value="js" />
           <Picker.Item label="Python" value="python" />
           <Picker.Item label="Haxe" value="haxe" />
         </Picker>
         {this.state.language == "haxe"?
         <Text>Hello Haxa</Text>
         :this.state.language == "js"?
         <Text>Helo JavaScript</Text>
         :this.state.language == "python"?
         <Text>Helo Python</Text>
         :this.state.language == "java"?
         <Text>Helo Java</Text>
         :<Text>Nothing</Text>}
       </View>
     );
   }
 }

 const styles = StyleSheet.create({
   container: {
     flex: 1,
     flexDirection: 'column',
     alignItems: 'center',
     padding: 20,
     backgroundColor: 'white',
   },
   title: {
     fontSize: 18,
     fontWeight: 'bold',
     marginTop: 20,
     marginBottom: 10,
   },
   picker: {
     width: 200,
     backgroundColor: '#FFF0E0',
     borderColor: 'black',
     borderWidth: 1,
   },
   pickerItem: {
     color: 'red'
    },
   onePicker: {
     width: 200,
     height: 44,
     backgroundColor: '#FFF0E0',
     borderColor: 'black',
     borderWidth: 1,
   },
   onePickerItem: {
     height: 44,
     color: 'red'
   },
   twoPickers: {
     width: 200,
     height: 88,
     backgroundColor: '#FFF0E0',
     borderColor: 'black',
     borderWidth: 1,
   },
   twoPickerItems: {
     height: 88,
     color: 'red'
   },
 });

暂无
暂无

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

相关问题 如何制作包装器组件以设置 react-native-picker 中的选择器项目样式? - How do I make a wrapper component to style a picker item in react-native-picker? @react-native-picker/picker 导致我的应用程序崩溃(android) - @react-native-picker/picker causes my app to crash (android) Picker 已从 React Native 中移除。 现在可以从“@react-native-picker/picker”安装和导入它 - Picker has been removed from React Native. It can now be installed and imported from '@react-native-picker/picker' 如何在 React-native-picker 中更改下拉菜单的背景颜色并且无法更改(当前显示灰色)。 那我该怎么办? - how can I can change background color of dropdown in React-native-picker and not able to change that(currently showing grey). So how can I? react-native-picker 不适用于 react-native 版本 0.63.2 - react-native-picker not working on react-native version 0.63.2 不变违规:选择器已从 React Native 中删除。 现在可以从 '@react-native-picker 安装和导入它 - Invariant Violation: Picker has been removed from React Native. It can now be installed and imported from '@react-native-picker 我如何使用 React Native 日期时间选择器 - How can I use react native datetime picker React Native:我如何获取 Picker 的值并将其添加到 Flatlist - React Native: how can i Get value of Picker and add it to Flatlist 如何根据所选的选择器值显示组件? [反应原生] - how to show a component depending of picker value selected? [React native] 如何在 React Native 中隐藏 Picker 的第一个 Picker Item? - How can i hide first Picker Item of Picker in React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM