I'm trying to get a value back from Formik. It works with a Textfield, but I don't get the value of my switcher back (which is a boolean).
I have my file and switcher setup like this:
<div className="side2">
<Field
type="email"
label={currentEmail}
name="email"
placeholder={t('register.email')}
style={{ width: '55%' }}
className="fontLogin"
component={FormikInput}
/>
</div>
</div>
<div className="main">
<div className="side">
<p>Is admin ?</p>
</div>
<div className="side2">
<Switch
name="admin"
id="admin"
checked={(open === 'true')}
onChange={() => handleChange(!(open)}
color="primary"
inputProps={{ 'aria-label': 'primary checkbox' }}
/>
</div>
</div>
And in my other file I use formik like this:
<Formik
ref={form}
initialValues={{ email: '', admin: '' }}
onSubmit={(values) => {
handleSubmit(values);
handleClose();
}}
render={(props) => (
<FormRender
t={t}
currentEmail={currentEmail}
currentAdmin={currentAdmin}
currentVerified={currentVerified}
activeButton
{...props}
/>
)}
/>
I tried to set the value of my switcher to a string with the .toString()
method, but it doesn't work.
How is it possible to get a switcher value back? What am I misunderstanding here?
Best,
Set the initial value to false/true
, then use the props.values.admin
within the Switch component, and use the setFieldValue Formik function to update the value.
<Formik
initialValues={{ email: '', admin: false }}
...
/>
<Switch
...
checked={props.values.admin}
onChange={() => props.setFieldValue("admin", !props.values.admin)}
...
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With