简体   繁体   English

组件正在将文本类型的受控输入更改为不受控制。 输入元素不应从受控切换到不受控

[英]A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled

This is SignIn Component .这是SignIn Component I am using Firebase concept.我正在使用 Firebase 概念。 Material UI for designing purpose.用于设计目的的材料 UI。 I am using functional component.我正在使用功能组件。

在此处输入图像描述

Is it proper way to authenticate the user?I facing an error.对用户进行身份验证的方法是否正确?我遇到了一个错误。

import React, { useState } from "react";
import Avatar from "@material-ui/core/Avatar";
import Button from "@material-ui/core/Button";
import CssBaseline from "@material-ui/core/CssBaseline";
import TextField from "@material-ui/core/TextField";
import LockOutlinedIcon from "@material-ui/icons/LockOutlined";
import Typography from "@material-ui/core/Typography";
import { makeStyles } from "@material-ui/core/styles";
import Container from "@material-ui/core/Container";

import Firebase from "../services/Firebase";


const Signin = () => {
  const [values, setValues] = useState({email: "",password: ""})

  const useStyles = makeStyles(theme => ({
    paper: {
      marginTop: theme.spacing(8),
      display: "flex",
      flexDirection: "column",
      alignItems: "center"
    },
    avatar: {
      margin: theme.spacing(1),
      backgroundColor: theme.palette.secondary.main
    },
    form: {
      width: "100%", // Fix IE 11 issue.
      marginTop: theme.spacing(1)
    },
    submit: {
      height: 48,
      padding: "0 15px",
      margin: theme.spacing(7)
    }
  }));
  
  const classes = useStyles();
  const signin = (e) => {
    e.preventDefault();
    Firebase.auth().signInWithEmailAndPassword(values.email, values.password).then((u) => {
      console.log(u)
    }).catch((err) => {
      console.log(err);
    })
  }

  const signup = (e) => {
    e.preventDefault();
    Firebase.auth().createUserWithEmailAndPassword(values.email, values.password).then((u) => {
      console.log(u)
    }).catch((err) => {
      console.log(err);
    })
  }

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

  return (
    <Container component="main" maxWidth="xs">
      <CssBaseline />
      <div className={classes.paper}>
        <Avatar className={classes.avatar}>
          <LockOutlinedIcon />
        </Avatar>
        <Typography component="h1" variant="h5">
          Sign in
        </Typography>
        <form className={classes.form} noValidate>
          <TextField
            variant="outlined"
            margin="normal"
            required
            fullWidth
            placeholder="Enter Email.."
            onChange={(e) => handleChange(e)}
            value={values.email}
          />
          <TextField
            variant="outlined"
            margin="normal"
            required
            fullWidth
            placeholder="Enter Password.."
            type="password"
            onChange={(e) => handleChange(e)}
            value={values.password}
          />

          <Button
            type="submit"
            margin="normal"
            variant="contained"
            color="primary"
            className={classes.submit}
            onClick={(e) => signin(e)}
          >
            Sign In
          </Button>
          <Button
            type="submit"
            margin="normal"
            variant="contained"
            color="primary"
            className={classes.submit}
            onClick={(e) => signup(e)}
          >
            Sign Up
          </Button>
        </form>
      </div>
    </Container>
  )
}

export default Signin;

This Firebase ComponentFirebase组件

import firebase from 'firebase';
require('firebase/app')
require('firebase/auth')

const Firebase = firebase.initializeApp({
  apiKey: "AIzaSyC_3KRb7H0Xw1-DGfqAzqfxeZaw3W5PaLg",
  authDomain: "my-login-page-react.firebaseapp.com",
  databaseURL: "https://my-login-page-react.firebaseio.com",
  projectId: "my-login-page-react",
  storageBucket: "my-login-page-react.appspot.com",
  messagingSenderId: "415587749418",
  appId: "1:415587749418:web:ee026252bc0a64c1a57d53"
});

export default Firebase;

Problem might be caused by the way you keep values.问题可能是由您保留值的方式引起的。 You handle 2 values with 1 hook.您使用 1 个钩子处理 2 个值。 When you call你打电话时

setValues({
  [e.target.name]: e.target.value
})

It probably overrides the previous values which had 2 values with 1 value, either e-mail or password so one of them becomes unidentified and它可能会覆盖先前具有 2 个值和 1 个值的值,电子邮件或密码,因此其中一个变得无法识别并且

 //This is an uncontrolled component
 <TextField
        variant="outlined"
        margin="normal"
        required
        fullWidth
        placeholder="Enter Email.."
        onChange={(e) => handleChange(e)}
        value={unidentified}
 />

Try to seperate your values as:尝试将您的价值观分开为:

 [email,setEmail] : useState("");
 [password,setPassword] : useState("")

This will delete the initial object key-pairs making the value={values.*} in TextField uncontrolled.这将删除初始的 object 密钥对,使TextField中的 value={values.*} 不受控制。

setValues({
      [e.target.name]: e.target.value
    })

To override keeping earlier object key pairs use spread operation -要覆盖保留较早的 object 密钥对,请使用扩展操作 -

setValues({...values,
      [e.target.name]: e.target.value
    })

暂无
暂无

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

相关问题 组件正在更改要控制的电子邮件类型的不受控制的输入。 输入元素不应从不受控制切换为受控制 - A component is changing an uncontrolled input of type email to be controlled. Input elements should not switch from uncontrolled to controlled 组件将文本类型的受控输入更改为不受控制 - A component is changing a controlled input of type text to be uncontrolled 组件正在更改要控制的文本类型的不受控输入? - A component is changing an uncontrolled input of type text to be controlled? 警告:组件正在将文本类型的受控输入更改为不受控制。 (React.js) - Warning: A component is changing a controlled input of type text to be uncontrolled. (React.js) 一个组件正在将 SwitchBase 的不受控制的检查 state 更改为受控制。 元素不应从不受控制切换到受控 - A component is changing the uncontrolled checked state of SwitchBase to be controlled. Elements should not switch from uncontrolled to controlled React 中的输入值问题(一个组件正在将密码类型的受控输入更改为不受控制。) - Problem with input values in React (A component is changing a controlled input of type password to be uncontrolled.) 组件将电子邮件类型的受控输入更改为不受控制 - A component is changing a controlled input of type email to be uncontrolled 电子邮件输入警告-组件将文本类型的受控输入更改为不受控制 - Email Input Warning - A component is changing a controlled input of type text to be uncontrolled React - 一个组件正在将受控输入更改为不受控制。 这可能是由于值从已定义更改为未定义引起的 - React - A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined 警告:组件正在将受控输入更改为不受控。 这可能是由于值从已定义变为未定义引起的, - Warning: A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM