开发者问题收集

TypeError:验证不是 react.js 中的函数

2021-08-22
4949

我尝试验证我的表单,但总是得到相同的错误, 这是表单的代码:

import React from "react";
import zxcvbn from "zxcvbn";
import logo from "../images/milinus.png";
import useForm from "./useForm";

function Signup() {
  //function for validation of the submited data from the form
  const validation = (values) => {
    let errors = {};

    //name validion
    if (!values.name.trim()) {
      errors.name = "Name is required";
    }

    //last name validtion
    if (!values.lastname.trim()) {
      errors.lastname = "Username is required";
    }

    //email validtion
    if (!values.email.trim()) {
      errors.email = "Email is required";
    } else if (!/\S+@\S+\.\S+/.test(values.email)) {
      errors.email = "Email is invalid";
    }

    //password validtion
    if (!values.pass.trim()) {
      errors.pass = "pass is required";
    } else if (values.pass < 8) {
      errors.pass = "PassWord need to be 8 characters or more";
    }

    //pass_conf
    if (!values.pass_conf.trim()) {
      errors.pass_conf = "pass_conf is required";
    } else if (values.pass_conf == !values.pass) {
      errors.pass_conf = "pass_conf is not match the Password";
    }

    return errors;
  }

  //custom hook for the form
  const { hadleChange, values, handleSubmit, errors } = useForm(validation);

  //function that conforme and indicaite the score of the pass word
  const confirm_ps = (e) => {
    const weak = document.querySelector(".weak");
    const muduim = document.querySelector(".muduim");
    const strong = document.querySelector(".strong");
    const indicater = document.querySelector(".indicater");
    let test = zxcvbn(e.target.value);

    weak.setAttribute("style", "background-color:white");
    muduim.setAttribute("style", "background-color:white");
    strong.setAttribute("style", "background-color:white");
    indicater.innerHTML = "";

    console.log(test.score);

    if (test.score === 0 || test.score === 1) {
      if (e.target.value == !null) {
        weak.setAttribute("style", "background-color:white");
        muduim.setAttribute("style", "background-color:white");
        strong.setAttribute("style", "background-color:white");
        indicater.innerHTML = "";
      }
      console.log(e.target.value);
      weak.setAttribute("style", "background-color:red");
      indicater.innerHTML = "Weak";
    } else if (test.score === 2 || test.score === 3) {
      weak.setAttribute("style", "background-color:yellow");
      muduim.setAttribute("style", "background-color:yellow");
      indicater.innerHTML = "Meduim";
    } else if (test.score === 4) {
      weak.setAttribute("style", "background-color:green");
      muduim.setAttribute("style", "background-color:green");
      strong.setAttribute("style", "background-color:green");
      indicater.innerHTML = "Strong";
    }
  };

  return (
    <div className="signup">
      <div className="logo">
        <img src={logo} alt="logo" />
        <p>CREER UN COMPTE</p>
      </div>
      <div className="inputs">
        <form className="form" onSubmit={handleSubmit}>
          <div className="form-input">
            <input
              type="text"
              name="name"
              id="name"
              placeholder="Nom"
              value={values.name}
              onChange={hadleChange}
            />
            <p className="errorname">{errors.name}</p>
          </div>

          <div className="form-input ">
            <input
              type="text"
              name="lastname"
              id="lastname"
              placeholder="Prenom"
              value={values.lastname}
              onChange={hadleChange}
            />
            <p className="errorlastname"></p>
          </div>

          <div className="form-input">
            <input
              type="text"
              id="username"
              name="username"
              placeholder="Username"
              value={values.username}
              onChange={hadleChange}
            />
            <p className="errorusername"></p>
          </div>

          <div className="form-input">
            <input
              type="text"
              id="email"
              name="email"
              placeholder="Email"
              value={values.email}
              onChange={hadleChange}
            />
            <p className="erroremail"></p>
          </div>

          <div className="form-input">
            <input
              type="password"
              id="pass"
              name="pass"
              placeholder="Mote de pass"
              onChange={confirm_ps}
            />
            <p className="errorpassword"></p>
          </div>

          <div className="form-input">
            <input
              type="password"
              id="pass_conf"
              className="conform"
              name="pass_conf"
              placeholder="conform le mote de pass"
              value={values.pass_conf}
              onChange={hadleChange}
            />
            <p className="errorpass_conf"></p>
          </div>

          <div className="progress">
            <span className="weak"></span>
            <span className="muduim"></span>
            <span className="strong"></span>
          </div>
          <div className="indicater"></div>
          <div className="wornings">
            <ul>
              <li>Letters majuscule et minuscule</li>
              <li>Plus de 8 characters</li>
              <li>Contiens au moin un chiffers ou symbol</li>
            </ul>
          </div>
          <button type="submite" className="signup-btn">
            S'INSCRIRE AND ACCEPTER
          </button>
        </form>
      </div>
    </div>
  );
}

export default Signup;

这是自定义钩子的代码:

import { useState, useEffect } from "react";

const useForm = (callback,validation) => {
  const { values, setValues } = useState({
    name: "",
    lastname: "",
    username: "",
    email: "",
    pass: "",
    pass_conf: "",
  });
  const [errors, setErrors] = useState({});
  const [isSubmitting, setIsSubmitting] = useState(false);

  const handleChange = (e) => {
    const { name, value } = e.target;
    setValues({
      ...values,
      [name]: value,
    });
  };

  const handleSubmit = (e) => {
    e.preventDefault();
    setErrors(validation(values));
    setIsSubmitting(true);
  };

  useEffect(() => {
    if (Object.keys(errors).length === 0 && isSubmitting) {
      callback();
    }
  }, [errors]);

  return { handleChange, handleSubmit, values, errors };
};

export default useForm;

当我点击提交按钮时,我得到这个错误: TypeError:验证不是一个函数

22 | 23 | const handleSubmit = (e) => { 24 | e.preventDefault();

25 | setErrors(validation(values)); | ^ 26 | setIsSubmitting(true); 27 | };

3个回答

您正在为钩子设置两个参数 - 一个回调函数和验证函数,并且您只传递了验证函数

useForm(validation)

请先传递回调函数,然后再传递验证函数

Nitsan Cohen
2021-08-22

因为您只传递了 callback ,而不是验证。您需要 decalre callBack 并将其传递到 useForm

const callBack = () => {...};
useForm(callBack, validation);
Viet
2021-08-22

您的 useForm 收到两个参数,而您在回调中只给它一个

const useForm = (callback,validation) 

结果,这里出现错误:

setErrors(validation(values));
Ryan Le
2021-08-22