React-table: Default Sort with Hooks

Created on 6 Nov 2019  路  2Comments  路  Source: tannerlinsley/react-table

Using v6? Yes

Describe the bug
Using your sandbox https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/sorting I tried to add a call to toggleSortBy. I get error:
Uncaught TypeError: Cannot read property 'sortDescFirst' of undefined
at eval (index.js:1)
at eval (index.js:1)
at basicStateReducer (react-dom.development.js:16426)
at updateReducer (react-dom.development.js:16482)
at updateState (react-dom.development.js:16621)
at Object.useState (react-dom.development.js:17344)
at Object.useState (react.development.js:1619)

To Reproduce
Go to sandbox. Replace lines 37-50 with:
const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow, toggleSortBy, } = useTable( { columns, data, }, useSortBy ) toggleSortBy('firstName', false);

Expected behavior
I want the sandbox to sort by firstName.

Most helpful comment

I solved default sorting as follows:

  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
  } = useTable(
    {
      columns,
      data,
      initialState: {
        sortBy: [
          {
            id: 'firstName',
            desc: false,
          }
        ]
      }
    },
    useSortBy
  )

All 2 comments

I solved default sorting as follows:

  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
  } = useTable(
    {
      columns,
      data,
      initialState: {
        sortBy: [
          {
            id: 'firstName',
            desc: false,
          }
        ]
      }
    },
    useSortBy
  )

@tannerlinsley How would you do this on the column definition?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dilipsundarraj1 picture dilipsundarraj1  路  3Comments

Abdul-Hameed001 picture Abdul-Hameed001  路  3Comments

esetnik picture esetnik  路  3Comments

tremby picture tremby  路  3Comments

monarajhans picture monarajhans  路  3Comments