English 中文(简体)
根据登记用户选择的类型修改形式领域—— 重新处理
原标题:Change the form fields according to the type of registration user choose - React

我的想法如下:在我的网站上,用户将能够登记产品或客户。 然而,这两个表格中一个部分的登记形式显然不同。 我想让用户选择他们想要做哪些登记、客户或产品,而根据他们的选择,所介绍的领域是不同的。 如何利用反应ook形来做到这一点? 感谢!

问题回答

你们应当努力这样做:

import React from  react ;
import { useForm, Controller } from  react-hook-form ;

function RegistrationForm() {
  const { control, handleSubmit, watch } = useForm();
  const selectedOption = watch( registrationType ); // Watch the dropdown value

  const onSubmit = (data) => {
    console.log(data);
    // Handle form submission based on the selected option (customer or product)
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <div>
        <label>Choose Registration Type:</label>
        <Controller
          name="registrationType"
          control={control}
          defaultValue="" // Set a default value
          render={({ field }) => (
            <select {...field}>
              <option value="">Select...</option>
              <option value="customer">Customer</option>
              <option value="product">Product</option>
            </select>
          )}
        />
      </div>

      {selectedOption ===  customer  && (
        <div>
          {/* Customer-specific fields */}
          <label>Customer Name:</label>
          <input {...register( customerName )} />
          {/* Add more customer-specific fields here */}
        </div>
      )}

      {selectedOption ===  product  && (
        <div>
          {/* Product-specific fields */}
          <label>Product Name:</label>
          <input {...register( productName )} />
          {/* Add more product-specific fields here */}
        </div>
      )}

      <button type="submit">Submit</button>
    </form>
  );
}

export default RegistrationForm;




相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签