Bind Picker to list of Picker.Item in React Native

后端 未结 6 1736
一向
一向 2020-12-24 14:05

I\'m sure there is a way to do this, but I just can\'t see how yet.

What I\'d like to do, is add a Picker component, but rather than hard-code the list of items, fet

6条回答
  •  盖世英雄少女心
    2020-12-24 14:48

    No Loops required, here how i did in my react native based project, similar to above answer to bind or add data dynamically in Picker

    import React, {Component} from 'react';
    import {Picker,View} from 'react-native';
    export default class SampleApp extends Component  {
    
        constructor(props){
            super(props);
            // HARD CODED DATA , YOU CAN REPLACE THIS DATA  WITH API CALL DATA IN ComponentDidMount() 
           //or in Constructor because countryData is not state.
            this.countryData = ["India","Pakistan","USA"];
            // STATE    
            this.state({
                selectedCountry : null
            });
        }
        // Our country list generator for picker
        countryList = () =>{
            return( this.countryData.map( (x,i) => { 
                  return( )} ));
        }
        // RENDER
        render() {
            return (
                
                     ( this.setState({selectedCountry : value}) )}>
                        { this.countryList() }
                    
                
            );
        }
    }
    

    Hope you find my example simple to understand ;)

提交回复
热议问题