【发布时间】:2020-03-27 20:33:45
【问题描述】:
这是我第一次使用 Formik,我面临以下问题: 我使用 Formik 文档中提供的 typescript starter 创建了这个表单,它可以工作,但是我想显示一条成功消息并在 axios 返回状态为 200 时删除该表单。
所以,
1. 如何定位axios调用内部的表单引用?通常这就像e.target 一样简单,但该事件似乎在 Formik 中不可用。
2. Formik如何访问表单的状态?切换成功消息。
完整代码在这里:https://codesandbox.io/s/throbbing-water-ffl2w
非常感谢。
<Formik
initialValues={{
firstName: "",
lastName: "",
email: ""
}}
// initialStatus={{ // resetForm(); resets this
// sent: "nope"
// }}
onSubmit={(
values: Values,
{ setStatus, setSubmitting, resetForm }: FormikActions<Values>
) => {
axios({
method: "post",
url: "https://getform.io/f/15faef97-5703-4799-930d-c3e698c99967",
data: { email: values.email, values }
}).then(r => {
setSubmitting(false);
setStatus("sent");
//resetForm();
console.log("Thanks!");
});
}}
render={() => (
<Form>
<label htmlFor="firstName">First Name</label>
<Field
id="firstName"
name="firstName"
placeholder="John"
type="text"
/>
<label htmlFor="lastName">Last Name</label>
<Field id="lastName" name="lastName" placeholder="Doe" type="text" />
<label htmlFor="email">Email</label>
<Field
id="email"
name="email"
placeholder="john@acme.com"
type="email"
/>
<button type="submit" style={{ display: "block" }}>
Submit
</button>
</Form>
)}
/>
【问题讨论】:
标签: reactjs typescript axios formik