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

179
Views
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 answers
Answer question

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 Report
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!