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.

mui-datatable doesnt rerender when component renders

See original GitHub issue

Expected Behavior

Table should rerender and set it’s “data” prop to its component state after rerendering component.

Current Behavior

i have mui datatable component which renders a table with data from remote api. First component fetches data from api in componentDidMount lifecycle method and then set’s that data to components state and renders table with all the columns and data which it get from the component state, but problem occurs when you leave that component and get back to it(or in other words when you rerender component), then display message saying : “Sorry, no matching records found” appears. My first thought was component didn’t rerender correctly and table “data” props wasn’t updated, but the problem is that component rerenders , component fetches the data from api sets it in component state successfully like before but this time internal table state is not updated, its empty even though component rerendered, i tried rerendering component with different methods using forceUpdate and other methods all the time component is rerendered succesffully but table did not, only refreshing the web page initialize table to render, in fact i have more than 10 components with different data which it successfully fetched from api and set in state, but after leaving and returning to same component in UI table “data” prop didn’t update

Steps to Reproduce (for bugs)

import React from ‘react’; import { withRouter } from ‘react-router-dom’; import MUIDataTable from “mui-datatables”; import CustomToolbar from ‘./CustomToolbar’; import axios from ‘axios’; import { CircularProgress, Typography } from ‘@material-ui/core’;

class example extends React.Component { state = { isLoading: false, columns: [], data: [] }

componentDidMount() {
	this.getData();
}

componentDidUpdate = (prevProps, prevState) => {
	if(prevProps.modelsLoaded !== this.props.modelsLoaded ){
		this.setState({columns: this.makeColumns()})
	} 
}	

getData = () => {
	this.setState({isLoading: true});
	axios.get(`API`).then(res => this.setState({data: res.data, isLoading: false}))
}

makeColumns = () => {
	return Object.keys(someProps).map(data=> (
		{
			name: data,
			label: data,
			options: {
				customFilterListRender: v => `${data}: ${v}`,
				filter: true,
				sort: true
			}
		}
	))
}



render() {
	const { isLoading } = this.state;
	let options = {
		print: false,
		download: false,
		serverSide: true,
		viewColumns: true,
		customToolbar : () => {
			return (
				<CustomToolbar/>	
			)
		},
		selectableRows: 'single'
	}
	
	return (
		<div>
			<MUIDataTable
				title={
				<Typography variant="h6">
					EXAMPLE
					{isLoading && <CircularProgress size={24} style={{marginLeft: 15, position: 'relative', top: 4}} />}
				</Typography>
				}
				data={this.state.data}
				columns={this.state.columns}
				options={options}
			/>
		</div>
	)
}

}

export default withRouter(Example);

Your Environment

Tech Version
Material-UI “3.9.2”
MUI-datatables “2.6.4”
React “16.8.2”
browser chrome
etc

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
gabrielliwerantcommented, Aug 8, 2019

There are some known issues with handling all your column data in state. What I would do is try instead to have a column object in your render function, and only update via state the parts of the column object that need to be updated/changed. Give that a try and see if it works for you.

1reaction
laurexascommented, Aug 9, 2019

I get your point, but hey thank you again for helping me out, problem was solved so there is no reason to keep this topic open. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I re-render MUI data table upon any user action in react
I solved it. By using a simple hook while putting the axios call in separate async function. when I user performs an action,...
Read more >
mui-datatables - npm
MUI-Datatables is a responsive datatables component built on Material-UI. It comes with features like filtering, resizable columns, ...
Read more >
Looking for react table/grid component that can re-render with ...
I've looked at just about every React table/grid component available: ag-Grid, sematable, fixed-data-table, react-data-grid, react-table…
Read more >
Using material-table in React to build feature-rich data tables
Table components are designed to help you solve these and other ... To render a table with material-table, you have to supply the...
Read more >
Server rendering - Material UI - MUI
When the server receives the request, it renders the required component(s) into an HTML string, and then sends it as a response to...
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