using useState to initialise the state with the props -using map function for rendering data initially
vikashkrish01
Posted on June 1, 2020
import React, { useEffect, useCallback } from 'react';
import MaterialTable from 'material-table';
export default function MaterialTableDemo(props) {
const {todos} = props;
console.log('hurray',todos);
const [state, setState] = React.useState({
columns: [
{ title: 'Topic', field: 'topic' },
{ title: 'Start Date', field: 'startDate' , type: 'date'},
{ title: 'End Date', field: 'endDate', type: 'date' },
{
title: 'Level', field: 'level', type: 'numeric'
},
{
title: 'Comments', field: 'comments'
}
],
// having problem HERE !!!!! NOT RENDERING THE DATA INITIALLY
data: todos.map((row)=>{
return row
})
});
//Table data is WORKING WITH useEffect and useCallback hooks
// const dataNeeded =useCallback(()=> props.todos.map((row,index)=>{
// return row;
// }),[props.todos]);
// useEffect(()=>{
// setState({
// ...state.columns,
// data: dataNeeded()
// })
// }, [dataNeeded])
return (
title="Editable Example"
columns={state.columns}
data={state.data}
/>
);
}
Posted on June 1, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024
November 28, 2024
November 27, 2024