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
Question:
I am using map which is a data structure introduced in JS in ES6.
I am using Picker for the dropdown. here's my code :
<Picker
selectedValue={this.state.branch}
style={styles.fieldPicker}
mode="dropdown"
onValueChange={(itemValue, itemIndex) =>
this.setState({branch: itemValue})
}>
{/* Data Binding */}
{this.state.branchData.forEach((name, id) =>
{
return <Picker.Item key={id} value={id} label={name} />;
})
}
</Picker>
I checked if name and id are being correctly accessed by logging it. The issue is that I am not able to see the dropdown getting populated.
I got the same error. The problem is not with picker but instead, with your code.
let items = props.category.map((item,index) => {
return ( <Picker.Item key={index} label={item.category} value={item.category} /> )
})
return(
<Picker style={styles.pickerStyle}>{items}</Picker>
);
Instead of using item.category
, i was using item.name
which caused my app to stop.
So check the value of your data.
IMPORTANT: if you try to iterate an array of numbers with map
then don't forget that label
property of Picker.Item
should be converted to string:
const pickerItems = [1,2,3].map(i => (
<Picker.Item label={i.toString()} value={i} />
));
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( <Picker.Item label={x} key={i} value={x} />)} ));
}
// RENDER
render() {
return (
<View>
<Picker
selectedValue={this.state.selectedCountry}
onValueChange={ (value) => ( this.setState({selectedCountry : value}) )}>
{ this.countryList() }
</Picker>
</View>
);
}
}
Hope you find my example simple to understand ;)
You should probably set up your initial state as the empty array, then call your service on componentWillMount or componentDidMount. I've set up something that works with some dummy data here, and pasted the code below.
'use strict';
var React = require('react-native');
var {
Picker,
Text,
View,
AppRegistry
} = React;
var PickerItem = Picker.Item;
var SampleApp = React.createClass({
getInitialState() {
return {
services: ['a', 'b', 'c', 'd', 'e'],
selectedService: 'a'
}
},
componentDidMount() {
setTimeout(() => {
this.setState({
services: [ 'one', 'two', 'three', 'four', 'five' ]
})
}, 3000)
},
render() {
let serviceItems = this.state.services.map( (s, i) => {
return <Picker.Item key={i} value={s} label={s} />
});
return (
<View>
<Text>Pick a service</Text>
<Picker
selectedValue={this.state.selectedService}
onValueChange={ (service) => ( this.setState({selectedService:service}) ) } >
{serviceItems}
</Picker>
</View>
);
}
});
AppRegistry.registerComponent('SampleApp', () => SampleApp);
You can try this one.
<Picker
selectedValue={this.state.facilityId}
style={{ width: '100%' }}
onValueChange={(itemValue) => this.setState({ facilityId: itemValue, facilityPicked: true })}>
{facilities.map((facility, i) => {
return <Picker.Item key={i} value={facility.id} label={facility.facility_name} />
})}
</Picker>
Here, facilities is a list of object having id and facility_name as key.