gpt4 book ai didi

javascript - react 福米克 : how to use custom onChange and onBlur

转载 作者:行者123 更新时间:2023-12-03 12:58:32 36 4
gpt4 key购买 nike

我从 formik 开始库用于 react ,我无法弄清楚 Prop handleChange和handleBlur的用法。

根据the docs ,handleBlur 可以设置为<Formik/> 上的 Prop 。 ,然后必须手动向下传递到 <input/>

我已经尝试过,但没有成功:(为了更清楚,我保留了有关handleBlur的代码)

import React from "react";
import { Formik, Field, Form } from "formik";
import { indexBy, map, compose } from "ramda";
import { withReducer } from "recompose";

const MyInput = ({ field, form, handleBlur, ...rest }) =>
<div>
<input {...field} onBlur={handleBlur} {...rest} />
{form.errors[field.name] &&
form.touched[field.name] &&
<div>
{form.errors[field.name]}
</div>}
</div>;

const indexById = indexBy(o => o.id);
const mapToEmpty = map(() => "");

const EmailsForm = ({ fieldsList }) =>
<Formik
initialValues={compose(mapToEmpty, indexById)(fieldsList)}
validate={values => {
// console.log("validate", { values });
const errors = { values };
return errors;
}}
onSubmit={values => {
console.log("onSubmit", { values });
}}
handleBlur={e => console.log("bluuuuurr", { e })}
render={({ isSubmitting, handleBlur }) =>
<Form>
<Field
component={MyInput}
name="email"
type="email"
handleBlur={handleBlur}
/>
<button type="submit" disabled={isSubmitting}>
Submit
</button>
</Form>}
/>;

这种方法有什么问题吗?实际上应该如何使用handleBlur和handleChange?

最佳答案

您需要从 Formik 中删除第一个 handleBlur,因为模糊事件仅在字段级别有效,并在 Field 元素中执行类似以下操作:

<Field
component={MyInput}
name="email"
type="email"
onBlur={e => {
// call the built-in handleBur
handleBlur(e)
// and do something about e
let someValue = e.currentTarget.value
...
}}
/>

参见https://github.com/jaredpalmer/formik/issues/157

关于javascript - react 福米克 : how to use custom onChange and onBlur,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48466920/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com