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

145
Views
Can't stop an infinite loop at nested component at useEffect hook

I am trying to curb useEffect related to nested components. Here are the components:
Parent (it receives data from API):

const ListOfLots = (props) => {
  const initial = {listLots: props.lots, form: props.form}
  const [lots, setLots] = useState(initial);

  useEffect(() => {
    setLots({
      listLots: props.lots,
      form: props.form
    });
  });

  return (
    <div>
      {
        lots.listLots.map(function(lot) {
          return <Lot key={lot.uuid} lot={lot} procForm={lots.form}/>
        })
      }
    </div>
  )
}

Nested:

const Lot = (props) => {
  const initial = {currLot: props.lot, form: props.form};
  const [lot, setLot] = useState(initial);
  let getWinningBid = (offers) => {
    for (let i = 0; i < offers.length; i++) {
      console.log("BOOM!!!");
      if (offers[i].isTrue === true) {
        return offers[i].pip;
      }
    }
  }

  return (
    ...
  )
}

While I am using no dependencies at parent's useEffect, I have got an infinite invoking of console.log("BOOM!!!"), that is, of course, unacceptable, but my Nested component rerendered. When I try to use the following type of dependencies at useEffect: [], [lots.form], [lots.listLots] or [lots.listLots.length] my Nested component is not rerendered: it stays blank. So the result is the following: I have an infinite useEffect loop or not-working(?) useEffect.
Is there any way in this case to handle the useEffect?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use

useEffect(() => {
    setLots({
      listLots: props.lots,
      form: props.form
    });
  }, [props.lots, props.form]);

This triggers the callback only if the value of props.lots, props.form is changed else it won't be triggered on every rerender as in case of no second argument.

A similar question here might help you find better explanations.

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!