Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 48980

Adding to state variable array

$
0
0

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

Read full topic


Viewing all articles
Browse latest Browse all 48980

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>