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

144
Views
React JS - How this data is set in the setState after promise is resolved
  const [user, setUser] = useState([]);
  useEffect(() => {
    fetch("https://reqres.in/api/users?page=2")
      .then((res) => res.json())
      .then(setUser);
    return () => {
      console.log("CleanUp Code");
    };
  }, []);

How the set State is automatiocally set with this method .then(setUser); --> How the state is set with this method any explanation

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

0

.then is designed so that you pass a function into it. When the promise resolves, your function will be called and will be passed in the value that the promise resolved to.

So for example, you can create a brand new function, such as (res) => res.json(), and pass that function in to be called when the promise resolves. Or you can pass in a function which already exists, such as setUser. And again, that function will be called when the promise resolves. Calling setUser sets the state.

about 4 years ago · Juan Pablo Isaza Report

0

 const [user, setUser] = useState([]);
  useEffect(() => {
    fetch("https://reqres.in/api/users?page=2")
      .then((res) => setUser(res.data))
      .catch(err=>res.json(message: err.message));
    return () => {
      console.log("CleanUp Code");
    };
  }, []);
about 4 years ago · Juan Pablo Isaza Report

0

const [user, setUser] = useState([]);
useEffect(() => {
fetch("https://reqres.in/api/users?page=2")
  .then((res) => res.json())
  .then((res) => setUser(res.data))
  .then(setUser);
return () => {
  console.log("CleanUp Code");
};}, [])
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!