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

74
Views
Concat vs push adding new array in react best practice

Many folks promote immutability because they use redux altogether with react, but I'm still seeing people using push instead of concat.

Take this code for example:

submitComment() {
  console.log('submitComment: '+JSON.stringify(this.state.comment))

  APIManager.post('/api/comment', this.state.comment, (err, response) => {
    if (err){
      alert(err)
      return
    }

    console.log(JSON.stringify(response))
    let updateList = Object.assign([], this.state.list)
    updatedList.push(response.result)
    this.setState({
      list: updatedList
    })
  })
}

in this case does it matter at all? What's the issue with push above?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Immutability is used in react and not just by redux. The state of the React component should not be mutated directly. According to the documents :

Never modify this.state directly, as calling setState() afterwards may override the mutation you made. Treat this state as if it were immutable.

Furthermore, immutability also helps in reconciliation. If the props are immutable, you can perform a shallow equality check to see if it has changed or not, and render accordingly.

In your code, updatedList is cloned into a new array using Object#assign . Now you can Array#push to the array, without changing the original. Using Array#concat is a bit shorter and more readable:

 const updatedList = this.state.list.concat(response.result);
over 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!