字段组件中的reduxForm验证问题错误:已超过最大更新深度

问题描述

错误消息是: 错误:已超过最大更新深度。当组件重复调用componentwillUpdate或componentDidUpdate内部的setState时,可能会发生这种情况。 React限制了嵌套更新的数量以防止无限循环。

仅当我在Field组件的validate属性添加 required 函数时,才会出现此问题。没有 required ,一切正常。我不明白为什么它不起作用...

import { Field,reduxForm } from "redux-form";
import React from "react";
import { email,minLength,required } from "../utils/formValidation";
import inputComponent from "./inputComponent";

let AuthForm = (props) => {
  const { handleSubmit } = props;
  const minLength8 = minLength(8);
  return (
    <form className='auth-form' onSubmit={handleSubmit} action='/projects'>
      <Field
        id='email'
        name='email'
        type='text'
        label='Email'
        component={inputComponent}
        placeholder='Email'
        validate={[required,email,minLength8]}
      />
      <Field
        id='password'
        name='password'
        label='Password'
        component={inputComponent}
        type='password'
        // validate={[minLength8,required]}
      />
      <button type='submit'>Sign in</button>
    </form>
  );
};

AuthForm = reduxForm({ form: "auth" })(AuthForm);
export default AuthForm;

验证功能

export const email = (value) =>
  value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)
    ? "Invalid email address"
    : undefined;

export const minLength = (min) => (value) =>
  value && value.length < min ? `Must be ${min} characters or more` : undefined;

export const maxLength = (max) => (value) =>
  value && value.length > max ? `Must be ${max} characters or less` : undefined;

export const required = (value) =>
  value || typeof value === "number" ? undefined : "required";

输入组件

const inputComponent = ({
  input,label,type,Meta: { touched,error,warning },}) => (
  <div>
    <label>{label}</label>
    <div>
      <input {...input} placeholder={label} type={type} />
      {touched &&
        ((error && <span>{error}</span>) ||
          (warning && <span>{warning}</span>))}
    </div>
  </div>
);

解决方法

当我将reduxForm组件的父包装到connect()函数中时,我对其进行了修复

,

在我的情况下,需要在表单之前创建最小长度,也就是在连接导入之后。不要在 AuthForm 函数中创建 minLength 函数

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...