【发布时间】:2019-04-21 06:11:34
【问题描述】:
我正在尝试创建一个每次用户输入字段时都会递增的函数。
我目前有一个父组件,其状态的计数器值设置为 0 。我的子组件是一个 Formik 容器组件,用于保存和处理我的字段。
//父组件
import React, { Component } from "react";
import Formik from "./Formik";
export default class BountyPage extends Component {
state = {
total: 0
};
render() {
return (
<div className="bountyPage">
<div className="total_container">
<p className="total_title pt-2">SERV</p>
<h1 className="total_sub">{this.state.total}</h1>
</div>
<Formik />
</div>
);
}
}
// 子组件
const Formik = ({ values, errors, touched, isSubmitting,
setFieldValue }) => (
<Form className="form">
{touched.lastName &&
values.lastName &&
touched.firstName &&
values.firstName ? (
<Row>
<Col className="result_text pulse animated p-4">
<b>Welcome</b>{" "}
<span
className="clickable_text"
onClick={async () => {
await setFieldValue("firstName", "");
await setFieldValue("lastName", "");
}}
>
{values.firstName} {values.lastName}
</span>
</Col>
</Row>
) : (
<Row className="glow_form p-1">
<Col md={6}>
<FormGroup>
<Label for="firstName">First Name</Label>
<Field
type="firstName"
name="firstName"
placeholder="ie. John"
className="form-control"
/>
{touched.firstName && errors.firstName && <p>
{errors.firstName}</p>}
</FormGroup>
</Col>
<Col md={6}>
<FormGroup>
<Label for="lastName">Last Name</Label>
<Field
type="lastName"
name="lastName"
placeholder="ie. Doe"
className="form-control"
/>
{touched.lastName && errors.lastName && <p>
{errors.lastName}</p>}
</FormGroup>
</Col>
</Row>
)}
<Row className="glow_form pt-1">
<Col>
<FormGroup>
<Label for="email">Bittrex Email Address</Label>
<Field
type="email"
name="email"
placeholder="ie. example@domain.com"
className="form-control"
/>
{touched.email && errors.email && <p>{errors.email}</p>}
</FormGroup>
</Col>
</Row>
<Row>
<Col className="col_container">
<FormGroup>
<div className="switch_container">
<p>I’m a business owner</p>
<label className="switch">
<Field type="checkbox" name="switch1" checked=
{values.switch1} />
<span className="slider round" />
</label>
</div>
</FormGroup>
{values.switch1 && (
<Row className="bounceInDown animated">
<Col md={6}>
<FormGroup>
<Field
type="text"
name="industryText"
placeholder="ie. Automotive"
className="form-control"
/>
</FormGroup>
</Col>
<Col md={6}>
<FormGroup>
<Field
type="number"
name="locationText"
placeholder="ie. 1-2"
className="form-control"
/>
</FormGroup>
</Col>
</Row>
)}
</Col>
</Row>
{values.firstName && values.lastName ? (
<Row>
<Col className="text-center">
{values.email ? (
<span>
<Button
type="submit"
disabled={isSubmitting}
className="submit_btn"
>
Review
</Button>
<hr />
</span>
) : (
<span>
<Button
type="submit"
disabled={isSubmitting}
className="submit_btn"
>
Submit
</Button>
<hr />
</span>
)}
</Col>
</Row>
) : (
""
)}
</Form>
);
export default withFormik({
mapPropsToValues({
firstName,
lastName,
email,
switch1,
industryText,
locationText,
checkbox
}) {
return {
firstName: firstName || "",
lastName: lastName || "",
email: email || "",
switch1: switch1 || false,
industryText: industryText || "",
locationText: locationText || "",
checkbox: checkbox || false
};
},
validationSchema: Yup.object().shape({
firstName: Yup.string()
.max(40, "Please enter no more than 40 characters")
.required("Please enter your first name"),
lastName: Yup.string()
.max(40, "Please enter no more than 40 characters")
.required("Please enter a last name"),
email: Yup.string()
.email("Please enter a valid email")
.required("Please enter an email")
}),
handleSubmit(values, { resetForm }) {
console.log(values);
resetForm();
}
})(Formik);
如何从我的父组件中的 formik 容器访问“值”,以便我可以增加计数器状态,以便我的动态计数器在每次字段完成时增加?我尝试创建一个回调函数,但由于我不知道 formik 组件容器是如何工作的,所以没有正确地向下传递函数。
【问题讨论】:
-
可以使用formik提供的handleChange事件
-
我可以访问该父组件中的 handleChange 来实现增量功能吗?
-
您需要深入了解父组件的 onFormikValueChange 类型的处理程序,并在实际 formik 函数的 handleChange 中调用它
-
这是我第一次使用 Formik,所以我有点不确定该怎么做。
-
这就是我正在尝试的。我将在我的父组件中创建一个增量函数,我想将该函数向下钻取到我的 formik 容器,然后使用它来查看是否可以传递值
标签: javascript reactjs forms function count