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

116
Views
How to push new column into array using setState

I am using an external library to build a Taskboard and what I want to achieve is to add a new column when clicking and add column button. I am struggling with adding the column, I have the newly created column but it doesn't get added to the array. What am I doing wrong and how can I insert the newly created column? Here is my code:


  onAddColumn = () => {
    const newColumn: TaskBoardColumnModel = {
      id: this.state.columnsData.length + 1,
      title: 'New Column',
      status: 'new',
      edit: true,
    };

    console.log(this.state.columnsData);

    this.setState({
      columsData: newColumn,
    });
    console.log(this.state.columsData);
  };
}

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

0

Your main issue is the way you update your state. This is what you have:

this.setState({
      columsData: newColumn,
    });

This piece of code will set the state to columsData: newColumn, which is wrong for a few reasons:

  • columsData should be columnsData
  • Your piece of code will remove all the other columns and replace it with only one. Which will fail the code because columnsData will become an object and not an array

Here is what it should be:

this.setState({
      columnsData: [...this.state.columnsData, newColumn],
    });

This will keep your current state, and add the new column to your existing list.

about 4 years ago · Juan Pablo Isaza Report

0

You have a typo in columsData. Additionally columnsData should be an array so you probably need to set the new state to the old one plus the new column.

e.g. something like:

this.setState({
    columnsData: [...this.state.columnsData, newColumn],
});
about 4 years ago · Juan Pablo Isaza Report

0

this.setState({

if(this.state.columnsData && this.state.columnsData.length>0)
{
    columnsData: [...this.state.columnsData, newColumn],
}
});

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!