I am trying to print the state in the console for debugging, but I get this error message:
Cannot read property 'petname' of undefined
What is the right way to print state in the console and why is this calling it a property?
export default class App extends Component<{}> {
constructor(props) {
super(props)
this.state = {
petname: '',
owner: ''
};
}
addPet() {
console.log("Button Pressed");
console.log(this.state.petname);
return (
//some logic
);
}
render() {
return (
<View style={styles.container}>
<View style={styles.inputStyle}>
<Text>Pet</Text>
<TextInput onChangeText={petname => this.setState({petname})} style={{width:100}} />
</View>
<View style={styles.inputStyle} >
<Button title="Add Pet" onPress={this.addPet} />
</View>
</View>
)
}
}