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

182
Vistas
Using `useLocation().search` in useEffect without infinite loop

I can't figure out a way to use query inside of useEffect without an infinite loop.

const query = new URLSearchParams(useLocation().search);

useEffect(() => {
  query.forEach((value, field) => {
    ...
  }
 }, [query]);

Any ideas?

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

0

You're creating a new instance of URLSearchParams on every render (which is an object type and therefore has a different identity on each render), so the useEffect will run on every render (causing an infinite loop if the useEffect triggers a rerender).

Instead you can either move the definition of query into a useMemo call or just define query in the useEffect if it's not used elsewhere.

For example:

const { search } = useLocation();

const query = useMemo(() => new URLSearchParams(search), [search]);

useEffect(() => {
  query.forEach((value, field) => {
    ...
  }
 }, [query]);

or

const { search } = useLocation();

useEffect(() => {
  const query = new URLSearchParams(search);
  query.forEach((value, field) => {
    ...
  }
 }, [search]);
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