samedi 22 juin 2019

Function called twice time when contain react-hook

I'm creating a form for sign-up. In this form there is a checkbox for terms and condition, when I click for accept I have no update.

//react hook
        const [check, setCheck] = useState({
        confirmPassword: null,
        terms: false
    })

//checkbox in formik form
<IonCheckbox color="primary" name="Terms" checked={props.values.terms} onIonChange={(e) => test(e) } onIonBlur={props.handleBlur} />


//handler for change
    const test = (e: any) => {
        console.log(e.target.checked)
        setCheck({ ...check, terms: e.target.checked });
        console.log(check)
    }

When I click the checkbox I got

`Signup.tsx:74 true
 Signup.tsx:73 test
 Signup.tsx:74 false
 Signup.tsx:76 {confirm_password: null, terms: false}
 Signup.tsx:76 {confirm_password: null, terms: false}`

It looks like you call twice the handler. I expect when I click I get the update of terms field.

Why does it behave so what is the problem? how can i solve?




Aucun commentaire:

Enregistrer un commentaire