类型错误:无法将 undefined 或 null 转换为自定义 Hooks 中发生的对象

维克多阿拉孔

晚安社区!

当我尝试使用我的自定义钩子时,我目前收到此错误。

这个自定义钩子有 3 个参数,一个初始状态,一个验证规则和一个动作函数。

目前验证我的数据显示为未定义。

//This is the custom Hook! 
import React, { useState, useEffect } from 'react';

const useValidacion = (stateInicial, validar, fn) => {

const [ valores, guardarValores ] = useState(stateInicial);
const [ errores, guardarErrores ] = useState({});
const [ submitForm, guardarSubmitForm ] = useState(false);

useEffect(() => {
    if(submitForm) {
        const noErrores = Object.keys(errores).lenght === 0;

        if(noErrores) {
            fn(); //Funcion que se este ejecutando
        }
        guardarSubmitForm(false);
    }
}, [errores])

// Funcion que se ejecuta cuando el usuario escribe

const handleChange = e => {
    guardarValores({
        ...valores,
        [e.target.name]: e.target.value
    })
}

//Funcion que se ejecuta cuando el usuario da submit

const handleSubmit = e => {
    e.preventDefault();

    const erroresValidacion = validar(valores);
    guardarErrores(erroresValidacion);
    guardarSubmitForm(true);
}

return {
    valores,
    errores,
    submitForm,
    handleChange,
    handleSubmit
}
}
 
export default useValidacion;

这些是规则:

export default function validarCrearCuenta(valores) {

let errores = {};

// Validar el nombre 

if(valores.nombre.trim() === '') {
    errores.nombre = "El nombre es obligatorio"
}

// Validar el correo 

if(!valores.email.trim() === '') {
    errores.email = "El correo es obligatorio"
} else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(valores.email)) {
    errores.email = "Correo no valido"
} 

// Validar contraseña

if(!valores.password === '') {
    errores.password = "La contraseña es obligatorio"
} else if(valores.password.lenght < 6) {
    errores.password = "La contraseña debe ser de al menos 6 caracteres"
}
}

我真的很感激这方面的帮助。

反引号

是否validar曾经返回 null 或 undefined?

很可能errores在某个时候变成 null 或 undefined,可能是当它被设置为validar(valores)in的返回值时handleSubmit

然后我们调用Object.keys(null)useEffect,并导致错误。

编辑:啊,是的,我看到该validar函数没有返回值。return errores在最后就好了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型错误:无法将 undefined 或 null 转换为对象

JavaScript 未捕获的类型错误:无法将 undefined 或 null 转换为对象

未捕获的类型错误:无法在推送时将 undefined 或 null 转换为对象(<匿名>)

未捕获的 TypeError:使用 fullCalendar 时无法将 undefined 或 null 转换为对象错误

bootstrap5 无法将 undefined 或 null 转换为对象

使用hasOwnProperty时无法将null或undefined转换为对象

无法将 undefined 或 null 转换为对象 Angular

无法将 undefined 或 null 转换为对象 reactjs

将对象类型强制转换为自定义类时发生System.InvalidCastException

将字典数组转换为自定义对象swift

自定义 FirebaseListAdapter 中的错误“无法转换类型的对象”

对于给出 TypeError 的空对象:无法将 undefined 或 null 转换为对象

如何将 JSON 对象转换为自定义 C# 类型对象?

如果包装对象,将Golang转换为自定义类型会失败

如何将查询结果中的对象转换为自定义类型

如何解决TypeError:无法将undefined或null转换为对象

创建 THREE.Points 返回无法将 undefined 或 null 转换为对象

反应异步调用抛出无法将undefined或null转换为Function.getOwnPropertyNames的对象

在 Function.prototype 中得到“TypeError:无法将 undefined 或 null 转换为对象”

TypeError:无法将 undefined 或 null 转换为对象 | 下一个认证

TypeError:无法将 undefined 或 null 转换为对象:为我指明正确的方向

无法将NULL转换为指针到对象的类型

无法将 java.lang.Object 转换为自定义对象

MySQL:选择整列,但将NULL转换为自定义字符串

Google Cloud Function JS错误:无法将未定义或null转换为对象

无法从 System.String 强制转换或转换为自定义对象或模型

React无法将未定义或null转换为对象

无法将未定义或null转换为对象GraphQL

无法将未定义或null转换为对象redux