reactjs - 如何在 React Hook Form v7 中使用单选按钮?

标签 reactjs ionic-framework yup react-hook-form

我正在开发一个 Ionic React App,我想使用 React Hook Form 和 Yup Resolvers 来提交表单。我的表单包含一个单选按钮和另外两个输入。
我在使用单选按钮时遇到了困难。我应该如何编写它以存储其值并提交?
您可以在下面找到我的组件。即使我选择了其中一个单选按钮,也不会存储任何值。

import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";

const validationSchema = yup.object().shape({
  firstName: yup.string().required("First name is required"),
  lastName: yup.string().required("Last name is required"),  
});

const AddUser = () => {

  const history = useHistory();
  const [type, seType] = useState("");
  const [firstName, setFirstName] = useState("");
  const [lastName, setLastName] = useState("");
 
const {
    register,
    handleSubmit,
    formState: { errors },
    getValues,
  } = useForm({
    mode: "onChange",
    resolver: yupResolver(validationSchema),
  });

  const onSubmit= () => {
   
    var data = {
            type: getValues("type"),
            firstName: getValues("firstName"),
            lastName: getValues("lastName"),
          };

    axios
      .post("/add-user", data)
      .then((response) => {
        return response.data;
      })
      .catch((error) => {
        setMessage(
          `You are already registered`    
        );
        setIserror(true);
       
      });
  };

  return (
    <React.Fragment>
   
      <IonPage className="ion-page" id="main-content">
     
        <IonContent className="ion-padding">
          <div>
            <h2>Add New User</h2>
            
            <form onSubmit={handleSubmit(onSubmit)}>
              
              <IonList>
                <IonRadioGroup
                 {...register("type")}
                >
                  <IonItem>
                    <IonLabel>Type 2</IonLabel>
                    <IonRadio value="2" slot="start" />
                  </IonItem>

                  <IonItem>
                    <IonLabel>Type 4</IonLabel>
                    <IonRadio slot="start" value="4" />
                  </IonItem>

                  <IonItem>
                    <IonLabel>Type 6</IonLabel>
                    <IonRadio slot="start" value="6" />
                  </IonItem>
                </IonRadioGroup>
              </IonList>
               
               <IonItem>
                <IonLabel position="floating">First name*</IonLabel>
                <IonInput
                {...register("firstName")}
                ></IonInput>
              </IonItem>

 
              <IonItem>
                <IonLabel position="floating">Last name*</IonLabel>
                <IonInput
                {...register("lastName")}
                ></IonInput>
              </IonItem>

              <br />
              <IonButton
                type='submit'
              >
                create
              </IonButton>
            </form>
          </div>
        </IonContent>
      </IonPage>
    </React.Fragment>
  );
};

export default AddUser;
包.json
"@hookform/resolvers": "^2.4.0",
"react-hook-form": "^7.1.1",
"yup": "^0.32.9"
任何帮助将非常感激!

最佳答案

您需要使用 Controller而不是 register包装 <IonRadioGroup>因为 register仅适用于简单的 ionic 成分,如 <IonInput /> .

<Controller
  control={control}
  name="type"
  render={({ field: { onChange, value } }) => (
    <IonList>
      <IonRadioGroup
        value={value}
        onIonChange={({ detail: { value } }) => onChange(value)}
      >
        <IonItem>
          <IonLabel>Type 2</IonLabel>
          <IonRadio value="2" slot="start" />
        </IonItem>
        <IonItem>
          <IonLabel>Type 4</IonLabel>
          <IonRadio slot="start" value="4" />
        </IonItem>
        <IonItem>
          <IonLabel>Type 6</IonLabel>
          <IonRadio slot="start" value="6" />
        </IonItem>
      </IonRadioGroup>
    </IonList>
  )}
/>
Edit React Hook Form - Ionic Radio Group
我还删除了 getValues您的内部电话onSubmit方法,因为这个函数的第一个参数已经提供了所有注册的表单值。

关于reactjs - 如何在 React Hook Form v7 中使用单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67435384/

相关文章:

javascript - 无法读取未定义的属性 'querySelectorAll'

cordova - 基于ionic的App如何获取设备的uuid和型号信息?

javascript - 通过 $http 检索数组并在 ion-list 的 ion-item 中查看

Formik,Yup-如何检查十进制数

reactjs - 仅 React Material Table 前端分页

javascript - 语义 UI React 项目图像作为链接

reactjs - 如何将 yup 与 React hook 形式一起使用?

reactjs - React Formik - 仅在表单提交时触发验证

javascript - 如何在 react-router v4 中设置事件链接的样式?

javascript - 使用类似于 onSelect() 的 onChange() 将选择值打印到控制台