I've a Formik form with an array of checkBoxes that behave like radio buttons and a callback function:
function handleChange(
e: ChangeEvent<HTMLInputElement>,
arrayHelpers: FieldArrayRenderProps
) {
let us: IMUserSetting = {name:"db", value:e.target.id}
let c = !(userSettings.filter(x => x.name==="db" && x.value == us.value).length>0)
if (arrayHelpers.form.values.userSettings.length===0){
arrayHelpers.push(us);
}
else {
arrayHelpers.pop();
arrayHelpers.push(us);
}
setChanged(c);
/*
if (c) {
setTimeout(tSetChange,4000)
} else {
setTimeout(tUnSetChange,4000)
}
*/
}
when the call to "setChanged(c)" is not commented out, I need to click twice the checkBox to change selection, while after first click works correctly. I have spent a lot of time trying to figure out why this happens, tried to use even setTimeout to set state and noticed that after timeout expire the flag is restored back to the first checked checkbox. This is my first React work, probably I'm missing something on states.
I setted up a mock on codesandbox https://codesandbox.io/s/testformiktypescript-m9uik3?file=/src/userDb.tsx if You would like to try.
Have someone experienced something similar?
Thank You in advance.