I can't display the content of my json file in my table. I get the headers but nothing in cells.
Here is my code:
import Table from './Table'
import Menu from "./menus.json"
export default function Display() {
const [menu, setMenu] = useState([Menu]);
const data = useMemo(() => [
{
Header: 'Id',
accessor: menu.id,
},
{
Header: 'Title',
accessor: menu.title,
},
{
Header: 'Invited',
accessor: menu.invited.name,
},
{
Header: 'Price',
accessor: menu.price,
},
], [])
return (
<Table data={menu} columns={normalizeData(data.result)}>
)
}
Just before the return i tried an Object.keys(data).map((key) => ({id: key,})); but doesn't work.
Thanks for your help !
The JSON data is an object:
{
"menus": [
{
"id": "Menu1",
"title": "Creamy pea soup topped with melted cheese and sourdough croutons.",
"price": 4,
"invited": [
{
"name": "Jose Luis",
"location": "LA"
},
{
"name": "Smith",
"location": "New York"
},
],
},
...
]
}
...
import JsonData from "./menus.json";
...
const { menus } = JsonData;
The accessor
property isn't correct.
accessor: String | Function(originalRow, rowIndex) => any
firstName
then its value would be read from row['firstName']
. You can also specify deeply nested values with accessors like info.hobbies
or even address[0].street
row => row.firstName
, then its value would be determined by passing the row to this function and using the resulting value.accessor
if you really know what you're doing.Use stingified accessors
const data = useMemo(
() => [
{
Header: "Id",
accessor: "id"
},
{
Header: "Title",
accessor: "title"
},
{
Header: "Invited",
accessor: "invited[0].name" // because invited is an array
},
{
Header: "Price",
accessor: "price"
}
],
[]
);
Or use a function
const data = useMemo(
() => [
{
Header: "Id",
accessor: row => row.id
},
{
Header: "Title",
accessor: row => row.title
},
{
Header: "Invited",
accessor: row => row.invited[0].name
},
{
Header: "Price",
accessor: row => row.price
}
],
[]
);