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

86
Views
Add new column on button click in React JS react-table

I need to add new column to react-table when button click, even after re-rendering table with flag Iam unable to add new column, can you please suggest on where am I gone wrong. Here is the executable sandbox code.

https://codesandbox.io/s/tannerlinsley-react-table-row-selection-forked-3gcwm?file=/src/App.js

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to keep a state for the columns and change it accordingly using the setter method. Try like below:

const intialColumns = [
  {
    Header: "Units",
    accessor: "units",
    width: 400
  },
  {
    Header: "Units1",
    accessor: "units1",
    width: 400
  }
];

function App() {
  const [columns, setColumns] = useState(intialColumns);

  const addColumn = () => {
    setColumns((prevCols) => [
      ...prevCols,
      {
        Header: "Units3",
        accessor: "units3",
        width: 400
      }
    ]);
  };
  const data = React.useMemo(() => makeData(100), []);

  return (
    <Styles>
      <button style={{ margin: "5px" }} onClick={addColumn}>
        click here to add column{" "}
      </button>
      <Table columns={columns} data={data} />
    </Styles>
  );
}

Codesandbox:

Edit tannerlinsley/react-table: row-selection (forked)

about 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!