I have a component in the page in which I want to fetch the latest state of the page by refreshing the component every 30 seconds,
for example in this data is coming from the server and the id and names are constantly added and removed. Instead of refreshing the page manually everytime can we do the refresh of the component Datatable every 30 sec so as to ensure we have the latest data.
How can I do this. Is setInterval the right way or ShouldComponentUpdate will do the trick ?
Edit : Need to just edit the table instead of the whole component
export class DataTable extends React.Component {
constructor(props) {
super(props);
}
componentWillMount() {
this.context.store.dispatch(getData());
}
componentDidMount(){
this.updateTimer = setInterval(() => this.context.store.dispatch(getData()), 30000);
}
componentWillUnmount(){
clearInterval(this.updateTimer);
}
_getDtableProps() {
const arr = this.props.getData.data;
const data = arr.map((d, i) => {
return {
'id': this.formatPathId(d['id']),
'name': this.formatTime(d['name'])
}
})
return {
data,
dtOptions: {
order: [2, "desc"]
},
columns: [
{
data: 'id',
title: 'Id',
searchable: true
},
{
data: 'name',
title: 'Name',
searchable: true
}
]
}
}
render() {
let tableProps = this._getDtableProps();
return (
<div className={style.container}>
<Table {...tableProps}/>
</div>
);
}
}