Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

383
Views
React-Hook Form: How to make a reusable component for Autocomplete which will be used inside the useFieldArray?

I have this Autocomplete component

const Select = ({ name, control, defaultValue }) => {
  return (
    <div>
      Select
      <Controller
        as={
          <Autocomplete
            id="product"
            multiple
            style={{ width: 300 }}
            options={name}
            autoHighlight
            renderInput={(params) => (
              <TextField
                {...params}
                label="Product"
                variant="outlined"
                fullWidth
              />
            )}
          />
        }
        name="country"
        control={control}
      />
    </div>
  );
};

export default Select;

This is the FieldArray where I will be using this component:

const FieldArray = ({ control, register, setValue, getValues }) => {
  const { fields, append, remove, prepends } = useFieldArray({
    control,
    name: "order"
  });

  renderCount++;

  return (
    <div>
      <ul>
        {fields.map((item, index) => {
          return (
            <li key={item.id}>
              <Select control={control} />
              <select {...register(`order.${index}.product`)}>
                <option value="shirt1">shirt1</option>
                <option value="shirt2">shirt2</option>
                <option value="shirt3">shirt3</option>
              </select>

              <button type="button" onClick={() => remove(index)}>
                Delete
              </button>
              <NestedArray nestIndex={index} {...{ control, register }} />
            </li>
          );
        })}
      </ul>
      <section>
        <button
          type="button"
          onClick={() => {
            append({ name: "append" });
          }}
        >
          Add product
        </button>
      </section>
    
    </div>
  );
};

export default FieldArray;

For now, I have commented the Select in my codesandbox because this will always cause an error that says e.render is not a function

How can I make a reusable AutoComplete component?

Codesandbox: https://codesandbox.io/s/react-hook-form-wizard-form-complete-nnew-hvu5t8?file=/src/fieldArray.js:498-531

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!