提交时清除 Formik 表格不起作用,我做错了什么?
Clear Formik form on submission not working, what am i doing wrong?
我正在尝试找到一种在提交时清除此表单的方法,但我尝试过的任何方法都没有帮助。对于我做错了什么的任何见解,我将不胜感激!
我已经尝试添加重置和 onClick 事件以将数据设置回初始值,但我所做的一切似乎都不起作用。
FormJr.js
import React from "react";
import { useFormik } from "formik";
import axios from "axios";
const Form = ({ load, setLoad }) => {
const formik = useFormik({
initialValues: {
name: "",
date: "",
comment: ""
},
onSubmit: values => {
setLoad(load !== true ? true : false);
axios
.post(`https://lq-time-tracking.firebaseio.com/${name}.json`, values)
.then(function(response) {})
.catch(function(error) {
console.log(error);
});
}
});
return (
<form onSubmit={formik.handleSubmit}>
<label htmlFor="name">Name</label>
<select
id="name"
name="name"
type="name"
onChange={formik.handleChange}
value={formik.values.name}
>
<option value=""> Select One...</option>
<option value="Ryan">Ryan</option>
<option value="Patrick">Patrick</option>
<option value="Jennifer">Jennifer</option>
<option value="Dan">Dan</option>
</select>
<label htmlFor="date">Date</label>
<input
id="date"
name="date"
type="date"
onChange={formik.handleChange}
value={formik.values.date}
/>
<label htmlFor="comment">Comment</label>
<input
id="comment"
name="comment"
type="comment"
onChange={formik.handleChange}
value={formik.values.comment}
/>
<button type="submit">Submit</button>
</form>
);
};
export default Form;
在你的 onSubmit()
中尝试 formik.resetForm()
const formik = useFormik({
initialValues: {
name: "",
date: "",
comment: ""
},
onSubmit: values => {
formik.resetForm()
}
});
我正在尝试找到一种在提交时清除此表单的方法,但我尝试过的任何方法都没有帮助。对于我做错了什么的任何见解,我将不胜感激!
我已经尝试添加重置和 onClick 事件以将数据设置回初始值,但我所做的一切似乎都不起作用。
FormJr.js
import React from "react";
import { useFormik } from "formik";
import axios from "axios";
const Form = ({ load, setLoad }) => {
const formik = useFormik({
initialValues: {
name: "",
date: "",
comment: ""
},
onSubmit: values => {
setLoad(load !== true ? true : false);
axios
.post(`https://lq-time-tracking.firebaseio.com/${name}.json`, values)
.then(function(response) {})
.catch(function(error) {
console.log(error);
});
}
});
return (
<form onSubmit={formik.handleSubmit}>
<label htmlFor="name">Name</label>
<select
id="name"
name="name"
type="name"
onChange={formik.handleChange}
value={formik.values.name}
>
<option value=""> Select One...</option>
<option value="Ryan">Ryan</option>
<option value="Patrick">Patrick</option>
<option value="Jennifer">Jennifer</option>
<option value="Dan">Dan</option>
</select>
<label htmlFor="date">Date</label>
<input
id="date"
name="date"
type="date"
onChange={formik.handleChange}
value={formik.values.date}
/>
<label htmlFor="comment">Comment</label>
<input
id="comment"
name="comment"
type="comment"
onChange={formik.handleChange}
value={formik.values.comment}
/>
<button type="submit">Submit</button>
</form>
);
};
export default Form;
在你的 onSubmit()
formik.resetForm()
const formik = useFormik({
initialValues: {
name: "",
date: "",
comment: ""
},
onSubmit: values => {
formik.resetForm()
}
});