React-table: Accessor keys don't work with Dots

Created on 30 Nov 2019  路  5Comments  路  Source: tannerlinsley/react-table

Using v7?

Yawps!

Describe the bug

Table rows aren't able to access the columns in data when they have a . in the accessor key.

To Reproduce

With columns and data described below, the values for the broken column are undefined.

const columns = [
  {
    Header: 'broken column',
    accessor: 'first.column',
  },
  {
    Header: 'working column',
    accessor: 'first-column',
  },
]

const data = [
  {
     'first.column': 'hello world',
     'first-column': 'hello again',
  },
  {
    'first.column': 'fizz',
    'first-column': 'buzz',
  },
]

...
<Table columns={columns} data={data} />
...

Expected behavior

I expect the cell to still be able to access the data even with dot in the accessor key.

Codesandbox!

Use a new react-table codesandbox to reproduce the issue.

Screenshots

Screen Shot 2019-11-29 at 3 25 09 PM

Desktop (please complete the following information):

  • OS: iOS
  • Browser: chrome
  • Version: 79.0.3945.36 (Official Build) beta (64-bit)

Additional context

Not sure if this is by design or a limitation of my data model or a bug.

Most helpful comment

@burtyish thanks for the quick reply. Yea, I managed to fix it by using a custom accessor

{
    Header: 'First Column',
    accessor: (d) => d[key], // example key first.column
    id: key
}

Thanks for the hint!

All 5 comments

A period in an accessor is used for accessing properties on a data object.

Your column definition

const columns = [
  {
    Header: 'broken column',
    accessor: 'first.column',
  },
  {
    Header: 'working column',
    accessor: 'first-column',
  },
]

Would work well with data like this:

const data = [
    {
      first: { column: "hello world" },
      "first-column": "hello again"
    },
    {
      first: { column: "fizz" },
      "first-column": "buzz"
    }
  ];

Result:
image

You can see it in this working codesandbox: https://codesandbox.io/s/tannerlinsleyreact-table-basic-vxlcg

Oh! I see. Thank you for the clarification!

@tannerlinsley @burtyish is there any way to disable the deep inspection and treat the accessor as a normal string?

@kderbalah idk about that. But you can get full control of the logic by supplying a function as the accessor

@burtyish thanks for the quick reply. Yea, I managed to fix it by using a custom accessor

{
    Header: 'First Column',
    accessor: (d) => d[key], // example key first.column
    id: key
}

Thanks for the hint!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mlajszczak picture mlajszczak  路  3Comments

danielmariz picture danielmariz  路  3Comments

dilipsundarraj1 picture dilipsundarraj1  路  3Comments

dwjft picture dwjft  路  3Comments

Abdul-Hameed001 picture Abdul-Hameed001  路  3Comments