用useState和prev状态更新状态的问题

我的代码是这样的:

const [registryFormState, setRegistryFormState] = useState({
    email: null,
    username: null,
    password: null,
    confirmPassword: null,
  });

  const changeFormHandler = (value, inputIdentify) => {
    switch (inputIdentify) {
      case 'email':
        return setRegistryFormState((prevState) => ({
          ...prevState,
          email: value,
        }));
      case 'username':
        return setRegistryFormState((prevState) => ({
          ...prevState,
          username: value,
        }));
      case 'password':
        return setRegistryFormState((prevState) => ({
          ...prevState,
          password: value,
        }));
      case 'confirmPassword':
        return setRegistryFormState((prevState) => ({
          ...prevState,
          confirmPassword: value,
        }));
      default:
        return;
    }
  };

但是,当我仅更改email和console.log()的RegistryFormState时,所有属性值都更改为相同的值

评论