微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决字段组件中的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 函数

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。