I am trying to render input data within a state into a ListView. I get a TypeError: undefined is not an object.
import React, { Component } from 'react';
import { View, Text, Picker, StyleSheet, ListView } from 'react-native'
class BeerPicker extends Component {
constructor(props){
super(props);
this.state = {
beer = []
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}).cloneWithRows(['string1', 'string2', 'string3']),
};
this.addBeer = this.addBeer.bind(this);
}
addBeer(itemValue, itemIndex){
this.setState({
beer: [...this.state.beer, itemValue]
});
}
renderRow(data) {
return (
<Text>{`\u2022 ${data}`}</Text>
);
}
render() {
return (
<View>
<Picker selectedValue = {this.state.dataSource} onValueChange = {this.addBeer}>
<Picker.item label = 'Choose Beer' value = 'none' />
<Picker.Item label = "IPA" value = "ipa" />
<Picker.Item label = "Pilsner" value = "pilsner" />
<Picker.Item label = "Stout" value = "stout" />
</Picker>
<ListView
dataSource={this.state.beer}
renderRow={this.renderRow}
/>
</View>
)
}
}
export default BeerPicker;
const styles = StyleSheet.create({
text: {
fontSize: 30,
alignSelf: 'center',
color: 'black'
}
})
Before I had my state as just an empty array with the addBeer function binded to the constructor. Now I want to make that data render into a listview but now it doesnt work. Do I have to have the datasource binded to the constructor as well?
this.state.beerdoes not exist in the state since it has not been initialized in the constructor.