Home > front end >  React Native App closed when click on RNPickerSelect
React Native App closed when click on RNPickerSelect

Time:11-17

I want to add data in react native dropdown(RNPickerSelect). for that i have did this

let year = new Date().getFullYear() - 4;
        let Years = []
        for (var i = 0; i < 7; i  ) {
            let item = {
                id: i,
                value: year   i,
                label: year   i,
                key: year   i,
            };
            Years.push(item);
        }
        this.setState({
            Year: Years,
        })

but when i click on dropdown , app get closed automatically. thanks

CodePudding user response:

Label has to be of type string (sample).

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import RNPickerSelect from 'react-native-picker-select';

let year = new Date().getFullYear() - 4;
let Years = [];
for (var i = 0; i < 7; i  ) {
  let item = {
    id: i,
    value: year   i,
    label: year   i,
    key: year   i,
  };
  Years.push(item);
}

export default function App() {
  const [years, setYears] = React.useState(Years);

  return (
    <View style={styles.container}>
      <RNPickerSelect
        onValueChange={(value) => {
          
        }}
        items={Years.map((y) => {
          return { label: y.label.toString(), value: y.value }; // Convert numeric y.label to string
        })}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});
  • Related