React input中的Onchange导致在每个字母上呈现输入

  • 本文关键字:中的 input Onchange React reactjs
  • 更新时间 :
  • 英文 :


所以我在两个函数中创建了输入,在Onchange上,它获得了输入到它们中的值。但是每当我输入输入时,我每次只能输入一个字母,然后它就会失去焦点,我相信这是因为组件一直在呈现。我也意识到,每当我删除Onchange和值道具,然后一切都恢复正常,我能够轻松地键入一切。我该如何解决这个问题?

App.js

import style from "./auth.module.css";
import { useEffect, useRef, useState } from "react";
import { useAuthState } from 'react-firebase-hooks/auth';
import { auth, signInWithEmailAndPassword, signInWithGoogle, registerWithEmailAndPassword } from "../../firebase";
import { CSSTransition } from "react-transition-group";

function Auth() {
const [activeMenu, setActiveMenu] = useState("main");
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [user, loading, error] = useAuthState(auth);
const Emailform = () => {
return (
<div className={style.formBox}>
<label className={style.label}>Email:</label>
<form className={style.input}>
<input 
type="email" 
name="email" 
required="true"
value={email} 
onChange={(e) => setEmail(e.target.value)} />
</form>
</div>
);
}

const Passform = () => {
return (
<div className={style.formBox}>
<label className={style.label}>Password:</label>

<form className={style.input}>
<input 
type="password" 
name="password" 
required="true"
value={password} 
onChange={(e) => setPassword(e.target.value)} />
</form>
</div>
);
}
let domNode = useClickOutside(() => {
setActiveMenu(false);
});
return (
<div className={style.container}>
<Login />
<Signup />
</div>
);
function AuthType(props) {
return (
<a
href={props.link}
className={style.menuItem}
onClick={() => props.goToMenu && setActiveMenu(props.goToMenu)}
>
{props.children}
</a>
);
}

/* Login */
function Login() {
return (
<CSSTransition in={activeMenu === "main"} unmountOnExit timeout={500}>
<div ref={domNode}>
<div className={style.login}>
<h1 className={style.title}>Clip It</h1>
{/* Email and Password */}
<Emailform/>
<Passform/>
<div className={style.button}>
<input 
type="submit" 
value="Login"
onClick={() => signInWithEmailAndPassword(email, password)} />
<input 
type="submit" 
value="Login with Google"
onClick={signInWithGoogle} />
</div>
<div className={style.text}>
<p className={style.plink}>Forgot Password</p>
<div>
Need an account?&nbsp;
<AuthType goToMenu="Signup">click here</AuthType>
</div>
</div>
</div>
</div>
</CSSTransition>
);
}

function Signup() {
return (
<CSSTransition in={activeMenu === "Signup"} unmountOnExit timeout={500}>
<div ref={domNode}>
<div className={style.signUp}>
<div className={style.title}> Clip It</div>
<Form label="First Name" type="text" />
<Form label="Last Name" type="Text" />
<Form label="Email" type="email"/>
<Form label="Date of Birth" type="date" />
<Form label="Password" type="password"/>
<Form label="Confirm Password" type="password" />
<div className={style.button}>
<input type="submit" value="Sign Up" />
</div>
<div className={style.text}>
have an&nbsp;
<AuthType goToMenu="main"> account</AuthType>
</div>
</div>
</div>
</CSSTransition>
);
}
}
let useClickOutside = (handler) => {
let domNode = useRef();
useEffect(() => {
let clickListener = (event) => {
if (domNode.current && !domNode.current.contains(event.target)) {
handler();
}
};
document.addEventListener("mousedown", clickListener);
return () => {
document.removeEventListener("mousedown", clickListener);
};
});
return domNode;
};
function Form(props) {
return (
<div className={style.formBox}>
<label className={style.label}>{props.label}:</label>
<form className={style.input}>
<input 
type={props.input} 
name={props.input} 
required="true" />
</form>
</div>
);
}
export default Auth;

不要把你的组件定义放在其他组件中。如果你这样做,你在每次渲染时都创建一个新组件。

您需要将Emailform, Passform移到Auth之外。

签出这个沙箱进行实验

function Auth() {
// 🚫 the bad place to define components
const Emailform = () => {
...
}
// 🚫 the bad place to define components
const Passform = () => {
...
}
}

最新更新