Issue with react-table: TypeError: Cannot read property 'forEach' of undefined

Viewed 2203

I'm working on a react frontend which calls an API and attempts to take the return JSON data and put in a table format using react-table. I've been stumped for a couple days now on this. I'm using only functions and hooks with React. My main component is:

const [tableJSON, setTableJSON] = useState('');
const [inquiryCount, setInquiryCount] = useState(1);
const [sortByColumn] = useState('INQ_RECEIVE_DATE');
const [sortByDirection] = useState('DESC');

const mid = memberId['usr-memberid'];

    const getInquiryData = async () => {
        try {
            const resp = await INQUIRY_API.inquiryList({ "memberId": mid, inquiryCount, sortByColumn, sortByDirection }, token['usr-token']);
            setTableJSON(resp);
            //return (resp);
        }
        catch (error) {
            console.log("Data load failed: " + error);
        }
    }

    useEffect(() => {
        getInquiryData();
    }, [inquiryCount]);


...
...
...
...

<HomeTableRT tabledata={tableJSON} />

My HomeTableRT component looks like:

import React, { Fragment, useEffect, useMemo } from 'react'
import '../../css/table.css';
import { useTable, useSortBy } from 'react-table'

const columns = React.useMemo(() => [
        { Header: 'Submitted by', accessor: 'submittedBy' },
        { Header: 'Inquiry PDF', accessor: 'inquiryId_pdf', },
        { Header: 'Report Type', accessor: 'reportType', },],
        [])

//const data = React.useMemo(() => JSON.stringify(props.tabledata), [props.tabledata])
const data = React.useMemo(() => props.tabledata, [props.tabledata])

const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable({ columns, data }, useSortBy)

The last line throws the error TypeError: Cannot read property 'forEach' of undefined

I've been through the issues in react-table repo and it seems like this was raised as a problem in a version update but it seems long ago solved. I've search all over for other clues, but am stumped!

I'm using: react-table": "^7.2.0", "react-scripts": "^3.4.1",

Any help would be appreciated!

2 Answers

The same error occurs if some forgets to pass data property to the useTable hook.

In my case it was the incorrect parameter name.

const changes = ...;
const {
        getTableProps,
        getTableBodyProps,
        headerGroups,
        rows,
        prepareRow,
    } = useTable({ columns, changes });

The fix is:

useTable({ columns, data: changes });

This is not the exact solution for the asked question but might be handy for others.

mine was shouting

TypeError: data.forEach is not a function accessRowsForColumn node_modules/react-table/dist/react-table.development.js:1453

I wanted to create a table with just one row - I was passing an object into data, but it needed to be an object within an array, so I simply did

<Table
columns={columns}
data={[myObject]}
/>
Related