当我更新状态时我的输入没有更新值

发布于 2025-01-17 00:07:23 字数 2104 浏览 0 评论 0原文

我做了两个组件,一个是登录组件,一个是表单输入。

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

负佳期 2025-01-24 00:07:23

您错过了将 value 传递给 FormInput 组件中的输入,这就是它没有被清除的原因。

const FormInput = ({ handleChange, label, value, ...otherProps }) => {
  return (
    <div className="group">
      <input className="form-input" value={value} onChange={handleChange} {...otherProps} />
      {label ? (
        <label className={`${value.length ? "shrink" : ""} form-input-label`}>
          {label}
        </label>
      ) : null}
    </div>
  );
};

You missed to pass the value to input in your FormInput component that's why it is not getting cleared.

const FormInput = ({ handleChange, label, value, ...otherProps }) => {
  return (
    <div className="group">
      <input className="form-input" value={value} onChange={handleChange} {...otherProps} />
      {label ? (
        <label className={`${value.length ? "shrink" : ""} form-input-label`}>
          {label}
        </label>
      ) : null}
    </div>
  );
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文