Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
React Hook Form Too many re-renders. React limits the number of renders to prevent an infinite loop

I am trying to watch a field called party. after selecting those fields list of products will need to be rendered. I'm using react hook form where I used watch hook to subscribe to change.

<FormProvider {...methods}>
   <form onSubmit={handleSubmit(onSubmit)}>
   <Select
       ref={ref}
       options={partyOptions}
       value={partyOptions.find(
              (c) => c.value === value
       )}
       onChange={(val: any) => onChange(val.value)}
       name={name}
        />
       )}
      <div className="card-body row">
        <ProductContext.Provider value={[product, setProduct]}>
            <ProductList partyId={partyId} />
        </ProductContext.Provider>
      </div>
</form>

On productlist component, i passed partyId as props and try to access API according to the partyId

export function ProductList({ partyId }: { partyId: any }) {
const { control } = useFormContext(); // retrieve all hook methods

const [productOptions, setProductOptions] = useState([] as any);
const productQuery = useQuery("products", () => searchProduct(partyId), {
    enabled: partyId !== undefined,
});

if (productQuery.isFetched) {
  setProductOptions(
    productQuery.data.data.map((d: any) => ({
     value: d.id,
     label: d.name,
   }))
  );
}

I am getting Too many re-renders. React limits the number of renders to prevent an infinite loop. Is there any solution to not render multiple times?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You've to add more logic, setProductOptions is called at each react render. For example, you can use a useEffect to call setProductOptions only once.

export function ProductList({ partyId }: { partyId: any }) {
   const { control } = useFormContext(); // retrieve all hook methods

  const [productOptions, setProductOptions] = useState([] as any);
  const productQuery = useQuery("products", () => searchProduct(partyId), {
      enabled: partyId !== undefined,
  });


  useEffect(() => {
    if (productQuery.isFetched) {
      setProductOptions(
        productQuery.data.data.map((d: any) => ({
            value: d.id,
            label: d.name,
          })
        )
     );
   }
  }, [productQuery.isFetched, setProductOptions]);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your problem is here:

  if (productQuery.isFetched) {
    setProductOptions(
      productQuery.data.data.map((d: any) => ({
        value: d.id,
        label: d.name,
      })
    )
  );

Every time the component is rendered, it triggers another state update with setProductOptions, which then causes another render and the cycle repeats infinitely. Consider using a useEffect hook instead:

  useEffect(() => {
    if (productQuery.isFetched) {
      setProductOptions(
        productQuery.data.data.map((d: any) => ({
          value: d.id,
          label: d.name,
        })
      )
    }
  }, [productQuery.isFetched);

Some more info on useEffect is available here: https://reactjs.org/docs/hooks-effect.html

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda