How to make the row editable when clicking on an “Edit” button on a cell with react-table

I am making the interactive table using react-table on my application.

My goal is to make a table that can be editable per row when clicking on a button in a table cell.

I designed an EditableCell like the following.

import React, {useState} from "react";

export const EditableCell = ({
                                 value: initialValue,
                                 row: Row,
                                 column: {id, editable, state},
                                 updateItem, // This is a custom function that we supplied to our table instance
                             }) => {
    // We need to keep and update the state of the cell normally
    const [value, setValue] = React.useState(initialValue);
    const {index} = Row;

    const onChange = e => {

    // We'll only update the external data when the input is blurred
    const onBlur = () => {
        updateItem(index, id, value);

    // If the initialValue is changed external, sync it up with our state
    React.useEffect(() => {
    }, [initialValue]);

     * Event handler to make a row editable.
     * @param e
    const setRowEditing = (e) => {
        // TODO

    let retObj = null;
    if (isEditing && editable) {
        switch (id) {
                retObj = <input className="input-edit w-100" value={value} onChange={onChange} onBlur={onBlur}/>;
    } else {
        switch (id) {
            case 'action_btn':
                retObj = <>
                    <button className="btn btn-sm btn-info btn-sm-td" onClick={setRowEditing}>{ isEditing? "Save" : "Edit"}</button>
                retObj = <div>{value}</div>;
    return retObj;

export const defaultColumn = {
    Cell: EditableCell,

And my table’s definition is: (I used my EditableCell component)
export default function Table({ columns, data, updateItem }) {

    // Use the useTable Hook to send the columns and data to build the table
    const {
        getTableProps, // table props from react-table
        getTableBodyProps, // table body props from react-table
        headerGroups, // headerGroups, if your table has groupings
        rows, // rows for the table based on the data passed
        prepareRow // Prepare the row (this function needs to be called for each row before getting the row props)
    } = useTable({
    }, useBlockLayout, useRowState);

      Render the UI for your table
      - react-table doesn't have UI, it's headless. We just need to put the react-table props from the Hooks, and it will do its magic automatically
    return (
        <table className="data-table" {...getTableProps()}>
            { => (
                <tr {...headerGroup.getHeaderGroupProps()}>
                    { => (
                        <th {...column.getHeaderProps()}>{column.render("Header")}</th>
            <tbody {...getTableBodyProps()}>
            {, i) => {
                return (
                    <tr {...row.getRowProps()}>
                        { => {
                            return <td {...cell.getCellProps()}>{cell.render("Cell")}</td>;

In setRowEditing function, I am going to change the state of the current row or its cells so that cells in the row are rendered as the input field or whatever.

But I don’t know how to change the state properly.

Could you advise on this?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In your column array that you pass into react table you need to create a button who’s onClick function takes a callback to edit your data to add an isEditing: true so you will handle turning the row to edit mode from outside of the table. No need for setRowEditing in an editable cell.

function to set the table data with ‘isEditing’ property

const handleClickEditRow = (rowIndex) => {
    setTableData(prev =>, index) => ({...r, isEditing: rowIndex === index})))

in your columns

    accessor: '[editButton]',
    Cell: (cellObj) => <button onClick={() => handleClickEditRow(cellObj.row.index)}>Edit</button>

Method 2

I tried to achieve this feature/functionality by doing the following steps:

// leverage useState react hook.     
const [editableRowIndex, setEditableRowIndex] = React.useState(null);

Our initial value for the state variable is null – so that all the rows are not open for edit.

Pass the state variable and method to the useTable() root hook.
custom plugin hooks and other variables/methods maintaining the component state are returned from the table instance. These you can later retrieve from anywhere you want.

You can render the Editable Cell Component and custom plugin hook (edit hook, in this case) according to the state variables.

const {
    // all your hooks...
  } = useTable(
      // all your other hooks...
      **// pass state variables so that we can access them in edit hook later
      editableRowIndex, // index of the single row we want to edit 
      setEditableRowIndex // setState hook for toggling edit on/off switch**
    // other hooks... 
    (hooks) => {
      hooks.allColumns.push((columns) => [
        // other hooks such as selection hook
        // edit hook
          accessor: "edit",
          id: "edit",
          Header: "edit",
          Cell: **({ row, setEditableRowIndex, editableRowIndex })** => (
              onClick={() => {
                **const currentIndex = row.index;
                if (editableRowIndex !== currentIndex) {
                  // row requested for edit access
                } else {
                  // request for saving the updated row
                  setEditableRowIndex(null); // keep the row closed for edit after we finish updating it
                  const updatedRow = row.values;
                  console.log("updated row values:");
                  // call your updateRow API
              *{/* single action button supporting 2 modes */}*
              **{editableRowIndex !== row.index ? "Edit" : "Save"}**

An example can be found in the following links:

code sandbox link:

github repo link:

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x