简体   繁体   English

对象作为 React 子项无效(找到:object,键为 {})

[英]Objects are not valid as a React child (found: object with keys {})

I am having trouble on posting the error from data after submit incorrect mail and password.提交错误的邮件和密码后,我无法发布数据错误。 With debugger i'm seeing error text, but the page has error: Objects are not valid as a React child (found: object with keys {}).使用调试器我看到错误文本,但页面有错误:对象作为 React 子对象无效(找到:object,键为 {})。 If you meant to render a collection of children, use an array instead.如果您打算呈现子项集合,请改用数组。

import { signInWithEmailAndPassword } from 'firebase/auth';
import React, {useState} from 'react';
import auth from '../../Firebase/firebase';

const SignIn = (errorCode) => {
    
    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');
    const [errorText, setErrorText] = useState(false);

   

    const signIn = (e) => {
        e.preventDefault();
        signInWithEmailAndPassword(auth, email, password)
        .then((userCredential) => {
            console.log(userCredential);
        })
        .catch((error) => {
            let errorCode = error.code;
            let errorMessage = error.message;
            
            debugger;
            console.log(errorCode)
            
            return (
            setErrorText(true))
           
        })

    }
  return (
    <div>
        <form onSubmit={signIn}>
            <h1>Hey, Log In</h1>
            <input type='email' placeholder='enter email' value={email} onChange={(e) => setEmail(e.target.value)}></input>
            <input type='password' placeholder='enter pass' value={password} onChange={(e) => setPassword(e.target.value)}></input>
            
            { errorText ? <div>{errorCode}</div> : null 
   
            }
            
            <button type='submit'>Push to login</button>
            
        </form>
    </div>
  )
}

export default SignIn

I expected to image errorCode at the page signIn.我希望在页面登录时显示 errorCode。 Please, help me.请帮我。

Hey you can declare the error code in global scope using useState like below and can follow below thread for looking into this kind of errors https://stackabuse.com/bytes/fix-objects-are-not-valid-as-a-react-child-error-in-react/嘿,您可以使用如下所示的 useState 在全局 scope 中声明错误代码,并且可以按照以下线程来查看此类错误https://stackabuse.com/bytes/fix-objects-are-not-valid-as-a-反应儿童错误反应/

 import React, { useState } from "react";

const SignIn = () => {
  const [errorCode, setErrorCode] = useState("");
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [errorText, setErrorText] = useState(false);
  
  

  const signIn = (e) => {
    e.preventDefault();
    setErrorCode('400')
    console.log(errorCode);
   setErrorText(true);
  };

  return (
    <div>
      <form onSubmit={signIn}>
        <h1>Hey, Log In</h1>
        <input
          placeholder="enter email"
          value={email}
          onChange={(e) => setEmail(e.target.value)}
        ></input>
        <input
          type="password"
          placeholder="enter pass"
          value={password}
          onChange={(e) => setPassword(e.target.value)}
        ></input>

        {errorText ? <div>Error code {errorCode}</div> : null}

        <button type="submit">Push to login</button>
      </form>
    </div>
  );
};

export default SignIn;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM