当前平台:NodeJS(最小),带有Redux的客户端React,Formik,Yup。
给定以下示例代码(由于与问题无关,因此不包括整个React Component代码):
class RegisterPage extends React.Component {
constructor(props) {
super(props);
}
// (...)
render () {
<Form>
<Field name="email" type="email" />
<Field name="password" type="password" />
<Field
name="myCheckbox"
type="checkbox"
checked={this.props.values.myCheckbox}
onChange={ ?????????? } />
</Form>
}
}
const handleFormSubmission = (values, { resetForm, setErrors, setSubmitting }) => {
console.log(values);
};
const handleFormChange = (event) => {
event.persist();
console.log('changed');
};
const MyFormik = withFormik({
mapPropsToValues ({ email, password, myCheckbox }) {
return {
email: email || '',
password: password || '',
myCheckbox: myCheckbox || false
}
},
validationSchema: (...Yup schema here...),
handleSubmit (values, bag) { return handleFormSubmission(values, bag); }
})(RegisterPage);
export default connect()(MyFormik);
Run Code Online (Sandbox Code Playgroud)
... 如何使用handleChange方法?我需要保留原始代码(来自Formik的代码),同时添加处理该复选框更改的代码。有一些组件行为取决于该复选框的选中值。
请注意,我没有将onChange道具传递给电子邮件或密码,因为没有多余的行为可以在上面进行编码。复选框是具有特殊行为的复选框。
您可以为复选框声明一个处理程序,然后使用它!使用Formkit的本机处理程序和您的自定义处理程序。
class RegisterPage extends React.Component {
constructor(props) {
super(props);
}
// (...)
handleCheckBox: (e) => {
// do whatever you want to the value
}
render () {
<Form>
<Field name="email" type="email" />
<Field name="password" type="password" />
<Field
name="myCheckbox"
type="checkbox"
checked={this.props.values.myCheckbox}
onChange={(e) => {this.props.handleChange(e); this.handleCheckBox(e)}} />
</Form>
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12713 次 |
| 最近记录: |