Skip to content

[v8] getValue should return a null value for nested keys #4499

Open
@dennemark

Description

@dennemark

Describe the bug

The issue is related to this issue: #4320 (comment)
The prior issue was solved by introducing an elaborate error message for undefined nested keys.

In a case, where some rows might have nested keys and others not, it is possible to catch the error of getValue, it is not nice though. Furthermore, this also adds up to other functions like getRowModel, when filters are used. So each function would need to get a try catch.

try { cell.getValue() } catch { console.error("🙈") }

The alternative approach would be, to create an entry for each nested key in each row. But it can be annoying for nested objects.

data.forEach((entry)=>{
  if(!(nestedKey0 in entry)){
     entry[nestedKey0] = {}
   }
   if(!(nestedKey1 in entry)){
      entry[nestedKey0][nestedKey1] = null;
   }
})

I would propose returning a null value, if the nested key is missing.

result = result[key]

for (const key of accessorKey.split('.')) {
   if(!(key in result)){
     return null
    }
    result = result[key]
    if (process.env.NODE_ENV !== 'production' && result === undefined) {
       throw new Error(
       `"${key}" in deeply nested key "${accessorKey}" returned undefined.`
        )
     }
}

Your minimal, reproducible example

https://codesandbox.io/s/dark-sky-pwysi7?file=/src/main.tsx

Steps to reproduce

See codesandbox.
Toggle line 29 comment in main.tsx and see how it works/fails.

I am not using columnHelper, since my use case has a nested Record<string, any> object without proper typing.

Expected behavior

If getValue() returns null for nested keys, it won´t fail.

How often does this bug happen?

No response

Screenshots or Videos

No response

Platform

  • all

react-table version

v.8.5.22

TypeScript version

No response

Additional context

I am not fully sure, if getValue() should return null or undefined, since I am not familiar enough with tanstack table and possible side effects.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions