I am trying to pass the value from 3 TextInput’s to a constructor on a button press. The values themselves are passing fine, I can verify that when I console.log() them. But the object property itself is having a type error thrown while I’m trying to set it’s value, which doesn’t make much sense to me. I’m inexperienced with JavaScript, but I’ve never seen a type error on something I’m trying to set the value of.
“Uncaught TypeError: Cannot set properties of undefined (setting ‘itemName’) at InventoryItem (InventoryItem.js:7:1)”
Taking the text Input:
const AddItem = props => {
const [itemName, setName] = useState('');
const [ExpirationDate, setExp] = useState('');
const [ReminderDate, setRem] = useState('');
return(
<View style={styles.header}>
<TextInput
style = {styles.input}
placeholder = "Enter Item Name"
onChangeText={(val) => setName(val)}/>
<TextInput
style = {styles.input}
placeholder = "Enter Expiration Date (mm/dd/yyyy)"
onChangeText={(val) => setExp(val)}/>
<TextInput
style = {styles.input}
placeholder = "Enter Reminder Date (mm/dd/yyyy)"
onChangeText={(val) => setRem(val)}/>
<Text>Name: {itemName}, Expiration: {ExpirationDate}, Reminder: {ReminderDate}</Text>
<TouchableOpacity style={styles.btn} onPress={() => InventoryItem(itemName, ExpirationDate, ReminderDate)}>
<Text style={styles.btnText}> <Icon name="plus" size={20}/>Add Item</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.btn} onPress={() => props.navigation.navigate('Home')}>
<Text style={styles.btnText}>Return Home</Text>
</TouchableOpacity>
</View>
)
}
Constructor:
export function InventoryItem(name, ExpirationDate, ReminderDate) {
console.log("Here");
console.log(name)
console.log(ExpirationDate)
console.log(ReminderDate)
this.itemName = name;
this.ExpirationDate = ExpirationDate;
this.ReminderDate = ReminderDate;
console.log(this.itemName);
console.log(this.ExpirationDate);
console.log(this.ReminderDate);
}
I was initially casting the parameters themselves to strings, and trying other things like that before this.itemName = name and such, then I realized it wasn’t the passed values that are the problem, but “this.itemName = ” itself thats having the type error. And I’m not sure what to do with that.