1
all right? I’m a beginner in React Native and in this app I’m making I can’t call 'setState' in the method I created. The name of the method I created is called includingMed() and I call it here
constructor() {
super()
this.state = {
isVisible: false,
newMed: [],
medName: "",
medInitHour: null,
medTimes: null
}
}
addMed = () => {
if(!this.state.medName.trim() || !this.state.medTimes){
Alert.alert('Dados inválidos!','Verifique se todos os campos estão preenchidos.');
return
}
try {
this.setState({
newMed: this.state.newMed.push({
medName: this.state.medName,
medInitHour: this.state.medInitHour,
medTimes: this.state.medTimes
}),
medInitHour: null,
medTimes: null,
medName: ""
});
var include = new MedList();
include.includingMed(this.state.newMed);
this.setState({newMed: []});
ToastAndroid.show('Medicamento Adicionado!', ToastAndroid.SHORT);
}catch (ex){
ToastAndroid.show('Falha ao adicionar. Erro: '+ex, ToastAndroid.SHORT);
}
}
And down here is all his programming which is where setState() fails to run and returns me that message:
includingMed = newMed => {
const meds = [...this.state.meds];
meds.push({
id: Math.random(),
medName: newMed.medName,
medInitHour: newMed.medInit,
medTimes: newMed.medTimes
});
this.setState({ meds });
}
Please, I would really appreciate it if someone could help me in this app, it’s my first time kkk Thank you, hug!!
Hello Pablo, put the complete code of your file, to facilitate the analysis of the error!
– Lucas Souza