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

118
Views
Keep track of changes in React state

I have a provider that receives data prop, puts it in a state. Also, there are a few methods to manipulate that state.

I pass the state and the data prop to consumers, but every time I change the state, there is no difference between the prop and the state. I want to be able to see what changed so I could update that value.

import { createContext, useContext, useEffect, useState } from "react";

const TableContext = createContext({
  data: [],
  headings: [],
  onChangeCellContent: () => {},
});

const TableProvider = ({ data, headings, children }) => {
  const [tableData, setData] = useState(data);
  const [tableHeadings, setHeadings] = useState(headings);

  useEffect(() => {
    setData((previousData) => {
      return data.length !== previousData.length ? data : previousData;
    });
  }, [data]);

  const onChangeHeadingCell = ({ key, value }) => {
    setHeadings((oldHeadings) =>
      oldHeadings.map((heading) => {
        if (heading.key === key) {
          heading.title = value;
        }
        return heading;
      })
    );
  };

  const onChangeCellContent = ({ rowId, cellKey, value }) => {
    setData((previousData) =>
      [...previousData].map((row) => {
        if (row.id === rowId) {
          row[cellKey] = value;
          return row;
        }
        return row;
      })
    );
  };

  const onAddNewRow = (rowData) => {
    setData((oldData) => [...oldData, rowData]);
  };

  return (
    <TableContext.Provider
      value={{
        tableData,
        data,
        onChangeCellContent,
        onChangeHeadingCell,
        onAddNewRow,
        headings: tableHeadings,
      }}
    >
      {children}
    </TableContext.Provider>
  );
};

export default TableProvider;

export const useTable = () => {
  const context = useContext(TableContext);

  if (context === "undefined") {
    throw Error("Table provider missing");
  }

  return context;
};

Here is the change handler, it works, but it also changes the original data:


const Row = ({ data: row}) => {
  const { onChangeCellContent, headings, data } = useTable();

  ...


  // GIVES ME THE SAME VALUE WHEN I TRIGGER ONCHANGE 
  console.log(row.value, data.find((s) => s.id === row.id).value);

  return  <tr><td><select
              className="w-full h-full focus:outline-none"
              style={{
                backgroundColor: "inherit",
              }}
              value={row.value}
              onChange={(e) =>
                onChangeCellContent({
                  rowId: row.id,
                  cellKey: "value",
                  value: e.target.value,
                })
              }
            >...</select></td></tr>
about 4 years ago · Juan Pablo Isaza
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!