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

125
Views
why useEffect is called infinitely

In useEffect in my react component I get data and I update a state, but I don't know why the useEffect is always executed:

const Comp1 = () => {
    const [studies, setStudies]= useState([]);
    React.useEffect( async()=>{
          await axios.get('/api/expert/',
         )
        .then((response) => {
          setStudies(response.data.studies);     
    
        },  (error) => {
          console.log(error );
        })
          console.log("called+ "+ studies);
    
    },[studies]);
    return(
         <Comp2 studies={studies}/>
          )

}

Here is my second Component used in the first component...

 const Comp2 = (props) => {
        const [studies, setStudies]= useState([]);
        React.useEffect( ()=>{

              setStudies(props.studies)
        
        },[props.studies, studies]);


        return(
             studies.map((study)=>{console.log(study)})
    
    }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

EDIT

const Comp2 = (props) => {

  // for some brief time props.studies will be an empty array, []
  // you need to decide what to do while props.studies is empty.

  // you can show some loading message, show some loading status,
  // show an empty list, do whatever you want to indicate
  // progress, dont anxious out your users
  return (
    props.studies.map((study)=>{console.log(study)}
  )
}

You useEffect hook depends on the updates that the state studies receive. Inside this useEffect hook you update studies. Can you see that the useEffect triggers itself?

A updates B. A runs whenever B is updated. (goes on forever)

How I'd do it?

const Comp1 = () => {
  const [studies, setStudies]= useState([]);
  React.useEffect(() => {
    const asyncCall = async () => {
      await axios.get('/api/expert/',
      )
      .then((response) => {
        setStudies(response.data.studies);     
      }, (error) => {
        console.log(error );
      })
      
      console.log("called+ "+ studies);
    }
      
    asyncCall();
  }, []);

  return(
    <Comp2 studies={studies}/>
  )
}
about 4 years ago · Juan Pablo Isaza Report

0

useEffect() has dependency array which causes it to execute if any value within it updates. Here, setStudies updates studies which is provided as dependency array and causes it to run again and so on. To prevent this, remove studies from the dependency array. Refer: How the useEffect Hook Works (with Examples)

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!