firebase 对象作为React子级无效(找到:具有键的对象{})

kfgdxczn  于 2022-12-27  发布在  React
关注(0)|答案(1)|浏览(93)

我有麻烦后,提交不正确的邮件和密码发布数据的错误。与调试器我看到错误文本,但页面有错误:对象作为React子级无效(找到:键为{}的对象)。如果要呈现子级集合,请改用数组。

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

我期望图像errorCode在页面登录。请帮助我。

mwg9r5ms

mwg9r5ms1#

嘿,您可以使用useState在全局范围内声明错误代码,如下所示,并可以按照以下线程查看此类错误https://stackabuse.com/bytes/fix-objects-are-not-valid-as-a-react-child-error-in-react/

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;

相关问题