question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Table do not rerender after data change. When I update data second time, it works.

See original GitHub issue

I have data coming in as a prop like below. I usually click a button in the parent component to change the prop in the Performancetbl component. But when i click on the button once, table is not rerendering with new data. When I click on it one more time, it rerenders though. Why is that happening?

I tried to save props into a state variable state in Performancetbl component, but that did not change the behavior at all.

I also tried console.log(props.datas) to see if correct data is appearing the first time I click on the button. And it is indeed the correct value! Can you guys figure out why this is happening?

function Performancetbl(props) {   
    const options = { 
        ...
    };
    console.log(props.datas)
    return(
        <div style={{ maxWidth: "100%" }}>
            <MaterialTable
                title="Overall"
                data={props.datas}
                columns={props.columns}        
                options={options}
                components={props.components}
            />
        </div>
    );
}

export default Performancetbl;

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14

github_iconTop GitHub Comments

2reactions
Sheingcommented, Mar 24, 2020

^ you were really fast in replying. I saw your other live demo and figured out the solution. Thanks man. Also, using stateprovider from ‘Unstated-Next’ works really well with this table. Simpler version inside Try—>

setData(prevState=>{
       const dataCopy = [...prevState];
       const index = dataCopy.indexOf(oldData);
       dataCopy[index] = newData;
       return dataCopy;
})
2reactions
oze4commented, Mar 24, 2020

@Sheing I wouldn’t expect that to update the data… you have to update the state/data yourself…

Live demo here

import React, { useState } from "react";
import MaterialTable from "material-table";
import { makeStyles } from "@material-ui/styles";
import tableIcons from "./TableIcons.js";

const originalData = ["Rock", "Paper", "Scissors"].map(word => ({
  id: Math.floor(Math.random() * 300),
  name: word
}));

const useStyles = makeStyles({
  myEditIcon: {
    color: "red",
    fontSize: "60px"
  }
});

export default function AppTable() {
  const classes = useStyles();
  const [data, setData] = useState(originalData);

  return (
    <MaterialTable
      data={data}
      title="My Table"
      icons={{
        ...tableIcons,
        Edit: () => <tableIcons.Edit className={classes.myEditIcon} />
      }}
      columns={[
        {
          title: "Id",
          field: "id"
        },
        {
          title: "Name",
          field: "name"
        }
      ]}
      editable={{
        onRowUpdate: (newData, oldData) => {
          return new Promise((resolve, reject) => {
            try {
              // First make a copy of state, per best practices
              const dataCopy = [...data];
              // Find the index of the updated row - we have to use old data since
              // new data is not part of state yet
              const index = dataCopy.indexOf(oldData)
              // Update the found index with the new data
              dataCopy[index] = newData;
              // Update our state
              setData(dataCopy);
              // Since `onRowUpdate` has to return a Promise, resolve it
              resolve();
            } catch (err) {
              // Since `onRowUpdate` has to return a Promise, reject it if there is an error
              reject(err);
            }
          });
        }
      }}
    />
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to re-render react-table in .js when data changes ...
The following code below does NOT re-render, however console says that the context.data.exactval value is updated. const context = useContext( ...
Read more >
How to Update Data Without Rerendering an Entire Grid in ...
With immutable mode in our Syncfusion Angular Data Grid, you can easily update records without rerendering the entire grid component.
Read more >
React re-renders guide: everything, all at once
Comprehensive guide on React re-renders. The guide explains what are re-renders, what is necessary and unnecessary re-render, what can ...
Read more >
Data refresh in Power BI
Because Power BI copies the data, you must refresh the dataset to fetch changes from the underlying data sources. When a dataset is...
Read more >
Re-rendering Components in ReactJS
A second or subsequent render to update the state is called as re-rendering. React components automatically re-render whenever there is a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found