Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

385
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda