When i click on the button, it executes the “test” function but it only adds “2” to the state variable. Anybody knows why it doesnt add “1” and “2”?
const [errors, setErrors] = useState({isValid: true, errorMessages: []})
const addErrorMessage = async(errorMessage) => {
let joined = errors.errorMessages.concat(errorMessage);
setErrors({ isValid: false, errorMessages: joined });
}
const test = async () => {
await addErrorMessage('1');
await addErrorMessage('2');
}
return (
<IonPage>
<IonHeader>
<Header />
</IonHeader>
<IonContent fullscreen>
<IonList>
{!errors.isValid &&
errors.errorMessages.map((entry, index) =>
<IonItem key={index}>{entry.valueOf()}</IonItem>
)
}
</IonList>
<IonButton onClick={test}>
test
</IonButton>
</IonContent>
</IonPage>
);
};
2 posts - 2 participants