当我更新状态时我的输入没有更新值
我做了两个组件,一个是登录组件,一个是表单输入。
我将 props 传递给表单输入以呈现某些输入表单。
我还有一个状态来保存此输入的值(双向绑定)。
到目前为止渲染效果还不错。
当我提交表单并将状态设置为初始时出现问题,我在控制台日志中看到状态更改,但我的输入没有显示,它们仍然显示了我提交的最后一个值。
感谢您的帮助
这是我的 SignIn 组件:
const SignIn: React.FC = () => {
const [info, setInfo] = useState({
email: "",
password: "",
})
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
setInfo({
email: "",
password: "",
})
}
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { value, name } = event.target
setInfo({ ...info, [name]: value })
}
return (
<div className="sign-in">
<h2>I already have an account</h2>
<span>Sign in with your email and password</span>
<form onSubmit={handleSubmit}>
<FormInput
label="Email"
name="email"
type="email"
value={info.email}
handleChange={handleChange}
required
/>
<FormInput
label="Password"
name="password"
type="password"
value={info.password}
handleChange={handleChange}
required
/>
</form>
</div>
)
}
这是我的 FormInput 组件:
const FormInput: React.FC<Props> = ({ handleChange, label, value, ...otherProps }) => {
return (
<div className="group">
<input className="form-input" onChange={handleChange} {...otherProps} />
{label ? (
<label className={`${value.length ? "shrink" : ""} form-input-label`}>
{label}
</label>
) : null}
</div>
)
}
I made two components, one is sign in component and one is form input.
I pass props to form input to render some input form.
And I also had a state to save the value of this inputs (two way binding).
Rendering is fine till now.
The problem when I submit form and set state to initial, i saw the state change in console log but my inputs didn't display that, they still had displayed the last values that I submited.
Thanks for your help
This is my SignIn component:
const SignIn: React.FC = () => {
const [info, setInfo] = useState({
email: "",
password: "",
})
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
setInfo({
email: "",
password: "",
})
}
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { value, name } = event.target
setInfo({ ...info, [name]: value })
}
return (
<div className="sign-in">
<h2>I already have an account</h2>
<span>Sign in with your email and password</span>
<form onSubmit={handleSubmit}>
<FormInput
label="Email"
name="email"
type="email"
value={info.email}
handleChange={handleChange}
required
/>
<FormInput
label="Password"
name="password"
type="password"
value={info.password}
handleChange={handleChange}
required
/>
</form>
</div>
)
}
And this is my FormInput component:
const FormInput: React.FC<Props> = ({ handleChange, label, value, ...otherProps }) => {
return (
<div className="group">
<input className="form-input" onChange={handleChange} {...otherProps} />
{label ? (
<label className={`${value.length ? "shrink" : ""} form-input-label`}>
{label}
</label>
) : null}
</div>
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您错过了将
value
传递给FormInput
组件中的输入,这就是它没有被清除的原因。You missed to pass the
value
to input in yourFormInput
component that's why it is not getting cleared.