首页 > 解决方案 > react-hook-form 不发送 PasswordConfirm

问题描述

大家好,我正在使用 react-hook-form 库,这对我来说真的很棒!

但此时我有一些问题我不想发送 pwdConfirm 值,因为这个值让我 400 错误,所以我如何在没有 pwdConfirm 的情况下发送值?

我只想发送 id、pwd、昵称!

    const dispatch = useDispatch();
const { register, watch, errors, handleSubmit } = useForm();
const password = useRef();
password.current = watch('password');
const type = 'normal';
const onSubmit = (data) => {
    console.log(data);
    dispatch(signUpRequest(data));
};
useEffect(() => {});
return (
    <>
        <form
            onSubmit={handleSubmit(onSubmit)}
            className={styles.signup__form}
        >
            <label>email</label>
            <input
                name="email"
                type="text"
                ref={register({ required: true, pattern: /^\S+@\S+$/i })}
                placeholder="&#xf0e0;"
            />
            {errors.email && (
                <p className={styles.error__message}>
                   incorrect email
                </p>
            )}

            <label>password</label>
            <input
                name="password"
                type="password"
                ref={register({ required: true, minLength: 6 })}
                placeholder="&#xf09c;"
            />
            {errors.password && errors.password.type === 'required' && (
                <p className={styles.error__message}>
                    password plz
                </p>
            )}
            {errors.password && errors.password.type === 'minLength' && (
                <p className={styles.error__message}>
                   at least 6character
                </p>
            )}

            <label>password confirm </label>
            <input
                type="password"
                name="pwdConfirm"
                ref={register({
                    required: true,
                    validate: (value) => value === password.current,
                })}
                placeholder="&#xf09c;"
            />
            {errors.pwdConfirm && errors.pwdConfirm.type === 'required' && (
                <p className={styles.error__message}>
                   check your password
                </p>
            )}
            {errors.pwdConfirm && errors.pwdConfirm.type === 'validate' && (
                <p className={styles.error__message}>
                  not correct your password
                </p>
            )}

标签: reactjssubmitreact-hook-form

解决方案


我已经使用javascript从现有对象delete operator中删除密钥。pwdConfirm

const onSubmit = (data) => {
    delete data.pwdConfirm
    console.log(data);
    dispatch(signUpRequest(data));
};


推荐阅读