Meh*_*aza 5 forms reactjs react-native formik
我正在使用 useFormik 钩子进行数据处理。添加数据很好,但是当我想更新同一组件中的数据时,我需要将初始值更改为自定义对象,而不是使用空字符串。有没有使用 useFormik Hook 的 formik 方法来做到这一点?虽然我可以通过状态来实现这一点,但是一直使用 formik 会有什么好处呢?请帮忙!
const formik = useFormik({
initialValues: {
name: '',
post: '',
contact: '',
email: '',
password: '',
},
onSubmit: values => {
//handling submit
},
});
Run Code Online (Sandbox Code Playgroud)
形式:
<form className={classes.form} onSubmit={formik.handleSubmit}>
<Grid container spacing={2}>
<Grid item xs={12} sm={6}>
<TextField
autoComplete="name"
variant="outlined"
required
fullWidth
label="Name"
autoFocus
id="name"
name="name"
onChange={formik.handleChange}
value={formik.values.name}
/>
</Grid>
<Grid item xs={12} sm={6}>
<TextField
variant="outlined"
required
fullWidth
label="Post"
autoComplete="post"
id="post"
name="post"
onChange={formik.handleChange}
value={formik.values.post}
/>
</Grid>
//Submit button and form close etc
Run Code Online (Sandbox Code Playgroud)
谢谢
我找到了解决方案:For One field
const editEmployee = ()=> {
formik.setFieldValue("name","Imran");
alert("changed")
}
Run Code Online (Sandbox Code Playgroud)
对于多个:
formik.setValues({"name":"hey","post":"hoii"});
Run Code Online (Sandbox Code Playgroud)
或发送整个对象
| 归档时间: |
|
| 查看次数: |
7866 次 |
| 最近记录: |