3

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!

7
  • 1
    i don't see forEach being called anywhere on this post ? am i missing something. Commented Jul 13, 2020 at 19:09
  • @BARNOWL my guess is its enacted within useSortBy Commented Jul 13, 2020 at 19:17
  • Can you check to see what you're getting for columns and data Commented Jul 13, 2020 at 19:26
  • The forEach is in the useTable function from the react-table package: > 585 | data.forEach((originalRow, rowIndex) => 586 | accessRow(originalRow, rowIndex, 0, undefined, rows) Commented Jul 14, 2020 at 12:15
  • 1
    solved with a bit of a hack, created a function which I pass to use memo which transcribes the json into a hard coded version of the json elements. Hopefully I'll figure out something more reasonable and refactor later.... thanks all! Commented Jul 14, 2020 at 15:31

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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]}
/>

1 Comment

If this is the workaround that solved your problem, then mark it as accepted to help others know it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.