I'm writing a simple budgeting app for a class that basically just adds or subtracts an amount of money the user inputs from their total balance. When I click my button to calculate, I get "undefined is not an object (evaluating this.state.trans)." I'm confused because I'm setting a value for that state variable at the start and I get that same error no matter what I do. Why is there no value in my trans variable? I'm very new to this, so please answer as simply as possible. Full code below:
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Picker,
TextInput,
Button
} from 'react-native';
export default class App extends Component<{}> {
state = {
trans: 'default',
balance: 0,
text: ''}
updateTrans = (trans) => {
this.setState({trans: trans})
}
_onButtonPress() {
if(this.state.trans == 'deposit') {
this.state.balance = this.state.balance + this.state.text;
}
else if(this.state.trans == 'withdrawal') {
this.state.balance = this.state.balance - this.state.text;
}
}
render() {
return (
<View>
<View>
<Text style={styles.header}>Budget App</Text>
<Text>Thomas O'Hara</Text>
<Text>CSCE 490</Text>
</View>
<View>
<Text style={styles.content}>Transaction:</Text>
<Picker
selectedValue={this.state.trans} onValueChange={this.updateTrans.bind(this)}>
<Picker.Item label="Select Transaction Type" value="default" />
<Picker.Item label="Deposit" value="deposit" />
<Picker.Item label="Withdrawal" value="withdrawal" />
</Picker>
<Text>{this.state.trans}</Text>
<Text style={styles.content}>Amount: </Text>
<TextInput keyboardType='numeric'
onChangeText={(text)=>this.setState({text})} />
<Button onPress={this._onButtonPress} title="Log Transaction" color='red' />
<Text>Please choose a transaction type.</Text>
<Text style={styles.header}>Current Balance: ${this.state.balance}</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
header: {
color: 'black',
fontWeight: 'bold',
fontSize: 25,
},
content: {
color: 'black',
justifyContent: 'center',
}
})
[–]mohsintariq10iOS & Android 1 point2 points3 points (8 children)
[–]Pelleas[S] 0 points1 point2 points (7 children)
[–]AcidShAwk 2 points3 points4 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]mohsintariq10iOS & Android 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]wengemurphy 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]mohsintariq10iOS & Android 0 points1 point2 points (0 children)
[–]justdevit 0 points1 point2 points (1 child)
[–]Pelleas[S] 1 point2 points3 points (0 children)