Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
Why does react-table Column not accept a function as accessor?

I have encountered a small problem converting my app to typescript. Here is the (shortened) code:

I have a type with a simple string property:

export type Person = {
     birth_date: string
...

This is used in a custom table component using react-table:

import {useTable, useSortBy, Column} from 'react-table'

const PersonTable: FC<{persons: Person, columns: Column<Person>}> = ({ persons, columns }) => {

  const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
      useTable<Person>({ columns, data: persons }, useSortBy)
...

Because birth_date is formatted YYYY-MM-DD, I want to change it to something readable when defining the columns, where formatDate returns a string:

const columnsPersons = useMemo(() => [
    {
        Header: 'Birth date',
        accessor: (row: Person) => formatDate(row.birth_date)
    }
], [])

But when I set

<PersonTable columns={columnsPersons} ...

I get the following error (shortened):

Type '{ Header: string; accessor: string; }' is not assignable to type '{ accessor: "birth_date"; }'.
Types of property 'accessor' are incompatible.
Type 'string' is not assignable to type '"birth_date"'.

I know I can use a function for the accessor, because this works. But I don't know if I'm using and / or typing it correctly. Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It looks like you returned wider type from formatDate than the accessor expected. You should return from formatDate a 'birth_date' type, not string. Or describe the column like { 'what returned from formatDate': string } instead of { 'birth_date': string }

// 1.
const formatDate = (date) => return date as 'birth_date'

// 2. 
const formatDate = (date: string): 'another_birth_date' => { .... }

const PersonTable: FC<{persons: Person, columns: Column<{'another_birth_date': string}>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda