admin管理员组文章数量:1287932
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"
ponent={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,
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"
ponent={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,
Share Improve this question edited Dec 3, 2019 at 17:33 Jonathan Irvin 1,07210 silver badges19 bronze badges asked Dec 3, 2019 at 17:18 KanKan 5397 silver badges21 bronze badges1 Answer
Reset to default 10Set the initial value to false/true
, then use the props.values.admin
within the Switch ponent, 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)}
...
/>
本文标签: javascriptHow to make Formik return a booleanStack Overflow
版权声明:本文标题:javascript - How to make Formik return a boolean? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741322308a2372274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论