Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array index as an accessorKey in ColumnDef does not work #4815

Open
2 tasks done
naavis opened this issue Apr 19, 2023 · 4 comments · May be fixed by #5808
Open
2 tasks done

Array index as an accessorKey in ColumnDef does not work #4815

naavis opened this issue Apr 19, 2023 · 4 comments · May be fixed by #5808

Comments

@naavis
Copy link

naavis commented Apr 19, 2023

Describe the bug

The TanStack documentation describes that you can use array indices as the accessorKey value if your data is in the form of nested arrays: https://tanstack.com/table/v8/docs/guide/column-defs#array-indices

However, if you do that, the library will crash when rendering the table.

Your minimal, reproducible example

https://codesandbox.io/s/hungry-breeze-50qbjx

Steps to reproduce

Create a table by calling useReactTable set the data to nested arrays, and use numbers as the accessorKeys. Below is a React component that reproduces it:

const Table =() => {
  const table = useReactTable({
    data: [
      ['Hello', 1, 2, 3],
      ['World', 4, 5, 6],
    ],
    columns: [
      { header: 'Name', accessorKey: 0 },
      { header: 'Col1', accessorKey: 1 },
      { header: 'Col2', accessorKey: 2 },
      { header: 'Col3', accessorKey: 3 },
    ],
    getCoreRowModel: getCoreRowModel(),
  });

  return (
    <table>
      <thead>
        <tr>
          {table.getFlatHeaders().map((header) => (
            <th key={header.id}>
              {header.isPlaceholder
                ? null
                : flexRender(header.column.columnDef.header, header.getContext())}
            </th>
          ))}
        </tr>
      </thead>
      <tbody>
        {table.getRowModel().rows.map((row) => (
          <tr key={row.id}>
            {row.getVisibleCells().map((cell) => (
              <td key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</td>
            ))}
          </tr>
        ))}
      </tbody>
    </table>
  );
});

Expected behavior

The table renders normally and does not crash.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Windows 11, Chrome 112.0.5615.121

react-table version

8.8.5

TypeScript version

No response

Additional context

As a workaround you can make a string out of the number, and the table works as expected. I.e. the following works fine:

columns: [
  { header: 'Name', accessorKey: '0' },
  { header: 'Col1', accessorKey: '1' },
  { header: 'Col2', accessorKey: '2' },
  { header: 'Col3', accessorKey: '3' }
]

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
@fw6
Copy link

fw6 commented Apr 24, 2023

Hi buddy, your accessorKey type is incorrect. Just change it to a string.
accessorKey?: string & typeof TData

@naavis
Copy link
Author

naavis commented Apr 24, 2023

Hi! That is my point. The linked documentation says integers are fine: https://tanstack.com/table/v8/docs/guide/column-defs#array-indices So either the documentation is wrong or the code is wrong.

And I mentioned the type of the key under "Additional context" in my original post.

@C-Higgins
Copy link

This is still an issue. Additionally, when using the createColumnHelper, it will not accept string numbers like "0"; it only allows for actual numbers, which do not work.

@schardev
Copy link

Unfortunately, the issue still persists. I fixed it using an accessor function:

columnHelper.accessor((row) => row[0], { ... })

But it would be great if the maintainers update the documentation to help others avoid running into this bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants