0

I`m trying to use the Basic table example from React Table, however when I run the application, the Server page where the table needs to be loaded thrown an error TypeError: Object(...) is not a function

Top level function where I import the <Table/>

import React from "react";
import { Table } from "../Components/Table/table.component"

export const Servers = () => {

return(
  <Table/>
   )
 }

Table component

import React, {useTable, useMemo} from 'react'
import django_data from '../../django_data.json'
import { COLUMNS }   from "./../../colums";



export const Table = () => {

  const columns = useMemo(() => COLUMNS, [])
  const data = useMemo(() => django_data, [])

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

  return (
    <table {...getTableProps()}>
      <thead>
        {headerGroups.map(headerGroup => (
          <tr {...headerGroup.getHeaderGroupProps()}>
            {headerGroup.headers.map(column => (
              <th {...column.getHeaderProps()}>{column.render('Header')}</th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody {...getTableBodyProps()}>
        {rows.map((row) => {
          prepareRow(row)
          return (
            <tr {...row.getRowProps()}>
              {row.cells.map(cell => {
                return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
              })}
            </tr>
          )
        })}
      </tbody>
    </table>
  )
}

Columns data

export  const COLUMNS =  [
{
  Header: 'Server Name',
  assesor: 'name',
},
{
  Header: 'OS Version',
  assesor: 'os_release',
},

{
  Header: 'Kernel Version',
  assesor: 'kernel_version',
  
},

{
  Header: 'Minion Status',
  assesor: 'minion_status',
  
},
]

django_data.json

[
{
    "id":"CEB4FD81E47A01F6AF51E4115B9A6514",
    "url": "http://example.com/api/v1/servers/9689/",
    "name": "example",
    "fqdn": "example.com",
    "os_type": "Linux",
    "status": "In Service",
    "os_release": "Suse 11.4",
    "uptime": "4d:13h:31m:56s",
    "kernel_version": "3.0.101-108.117-default",
    "target_kernel_version": null,
    "minion_status": "Up",      
},

Error trace which is shown in the page.

     9 | const columns = useMemo(() => COLUMNS, [])
    10 | const data = useMemo(() => django_data, [])
    11 | 
  > 12 | const tableInstance = useTable({
    13 |   columns,
    14 |   data
    15 | })
4
  • 1
    What is useTable? I don't think it is a thing in react package. Commented Nov 27, 2020 at 15:37
  • 1
    React don't have 'useTable' hook. Commented Nov 27, 2020 at 15:38
  • that import should be { useTable } from 'react-table' Commented Nov 27, 2020 at 15:52
  • Hi, thanks for the answer, obviously after all of the looking, I didn`t saw the most obvious... Anyway, now it is wokring, but the only displayed data is the Headers. Nothing from django_data.json. Could you please advice why this could be? Commented Nov 27, 2020 at 16:45

1 Answer 1

1

Change -> import React, {useTable, useMemo} from 'react'

To -> import { useTable } from 'react-table'

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

5 Comments

Yes, thanks for the answer, but as I wrote on the post above, after the correct import, the only data that is rendered is Headers, nothing from django_data.json.
Looks like the django_data.json file is missing a closing square bracket.
nope, this is just the first element of the data, I also though it might be issue with file, so I change it to .js and export the data as const then import it with { django_data } also add styles, and now I have empty grid with headers
Maybe you need the columns array property like in the example.
Maybe you need to convert your json object into a javascript object. The example seems to be using a javascript object vs your example provided uses a json object.

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.