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

176
Views
React setState call inside a custom hook useEffect is not updating the state

I have a basic custom hook in that I am changing a state inside useEffect but state is not changing

Below Users component is calling custom hook and passing items prop

const Users = (props) => {
  const [users] = usePivot(props.items)
}

usePivot custom hook

const usePivot = (propItems) => {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    // this is printing in whenever propItems changes
    console.log("propItems in useUserPivot: ", propItems);
    
    // this is not setting
    setUsers(propItems);
  }, [propItems]);

  useEffect(() => {
    // this is not printing in console
    console.log("users: ", users);
  }, [users]);

  return [
    users
  ];

}

value of users is always [], it never changes.

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

users state was getting set again in another function with sorted list, which is getting called in useEffect. I commented that function call and then issue got solved. I am not sure why this issue would come because of that.

almost 4 years ago · Santiago Trujillo Report

0

I used your same code here and everything is fine

enter image description here

make sure you are passing items as props to your users component based on your code

   <Users items={[1,2,3]} />
almost 4 years ago · Santiago Trujillo 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!