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

130
Views
Why is my hook variable in React not holding the state I set it to?

I am using React and Redux. When my props.employees changes for the 1st time, useEffect runs and updates my teamsArr to the value of props.employees.

But my desire is to add the props.employees array to the teamsArr array that should already be filled with the previous props.employees array values.

But instead, whenever I change the value of props.employees, my teamsArr variable is initially empty and then changes the value to solely the current props.employees value.

I've been struggling to figure out how to solve this, so any help would be very welcome.

const Employees = (props) => {
    const [teamsArr, setTeamsArr] = useState([])


    useEffect(() => {
        const updatedTeamsArr = [...teamsArr, props.employees]
        setTeamsArr(updatedTeamsArr)
        }
    ,[props.employees])
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can take advantage of passing a function to setTeamsArr

useEffect(() => {
    setTeamsArr(prev => [...prev, ...props.employees])
    }
,[props.employees])

but in this case everytime prop.employees changes it'll be added but old values won't be removed.

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!