How to dynamically load JSON data to React Native Picker

In this React Native source code example, the source code below illustrate how to dynamically load JSON data to React Native Picker.

You can copy and adopt this source code example to your React Native project without reinventing the wheel.

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

const data=[
  {"name":"stephen"},
  {"name":"kate"},
  {"name":"michael"}
]
export default class App extends Component {

   render() {
      return (
         <View style={styles.container}>
           <View style={{width:'60%',flexDirection: 'row',justifyContent: 'center'}}>
             <Picker mode="dropdown" style={{ height:20, backgroundColor: 'white',width: '80%'}}>
               {
                 data.map((item) =>{
                   return(
                   <Picker.Item  label={item.name} value={item.name} key={item.name}/>
                   );
                 })
               }
             </Picker>
           </View>
         </View>
      )
   }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#ddd'
  },
})

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment