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

101
Views
can someone explaine how setstate work inside useffect

i find a code that show loading before fetching data and shows "downloaded " after the fetching is ended the code is working ,however i couldn't understand how this code is working for my understanding the useEffect() will run one time when the app start and usestate() will rerender the component every time loading value changed so ,in the beginning of the code the useeffect() will run ,and setloading willchange the value to true and rerender the component ,for my point of vue it look like there is infinite loop of rendering can someone please explaine how usestate worke inside of useffect and how this code is working. this is the code:

import { useState } from "react";
import { useEffect } from "react";
export default function Assemble(){
const [loading,setloading]=useState(true);
useEffect(()=>{
   setloading(true);
      fetch('https://jsonplaceholder.typicode.com/photos')
  .then(response => response.json())
  .then(json => console.log(json))
  setloading(false);
},[])
   return(<div>
      <div>{loading?"Loading":"downloaded"}</div>
   </div>)
} ```

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

0

From my knowledge, the useEffect can get 2 arguments as parameters, 1 of them being the function that should be run, and the second one being an array (in your case it was []) which mentions upon the change of which dependencies should the first function be executed.

Now, because you used [] and left it empty, that means the useEffect will only run once.

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!