首页 > 解决方案 > 使用自定义组件时,onChange 处理程序不会触发

问题描述

我在 React 应用程序中使用Formik进行验证。

验证工作正常,但我的 onChange 处理程序没有触发:

  <Field
    type="text"
    name="name"
    placeholder="First Name"
    component={Input}
    onChange={() => console.log("gfdg")}
  />

沙盒链接

为什么是这样?

标签: javascriptreactjsformsonchangeformik

解决方案


在内部Input,您订购传递给输入元素的道具的方式意味着您onChange正在被 Formik 的onChange. 当您Field使用自定义组件(即Input在您的情况下)创建 a 时,Formik 将其传递FieldProps给组件。FieldProps包含一个field包含各种处理程序的属性,包括onChange.

在你的Input组件中你这样做(我已经删除了不相关的道具):

<input
    onChange={onChange}
    {...field}
/>

看看你自己的onChange将如何被 Formik 的onChange()内部取代field?为了更清楚...field,基本上是导致这种情况发生:

<input
    onChange={onChange}
    onChange={field.onChange}
    // Other props inside "field".
/>

如果您要重新排序这些控制台消息,现在将出现:

<input
    {...field}
    onChange={onChange}
/>

但是现在您的输入现在不起作用,因为您确实需要onChange在输入更改时立即调用 Formik 来让 Formik。如果您希望自定义onChange事件和输入正常工作,您可以这样做:

import React from "react";
import { color, scale } from "./variables";

const Input = React.forwardRef(
  ({ onChange, onKeyPress, placeholder, type, label, field, form }, ref) => (
    <div style={{ display: "flex", flexDirection: "column" }}>
      {label && (
        <label style={{ fontWeight: 700, marginBottom: `${scale.s2}rem` }}>
          {label}
        </label>
      )}
      <input
        {...field}
        ref={ref}
        style={{
          borderRadius: `${scale.s1}rem`,
          border: `1px solid ${color.lightGrey}`,
          padding: `${scale.s3}rem`,
          marginBottom: `${scale.s3}rem`
        }}
        onChange={changeEvent => {
          form.setFieldValue(field.name, changeEvent.target.value);
          onChange(changeEvent.target.value);
        }}
        onKeyPress={onKeyPress}
        placeholder={placeholder ? placeholder : "Type something..."}
        type={type ? type : "text"}
      />
    </div>
  )
);

export default Input;

在这里查看它的实际应用

虽然总的来说我不确定你想要做什么。您的表单工作正常,您可能不需要自定义onChange,但也许您有一些特定的用例。


推荐阅读