table icon indicating copy to clipboard operation
table copied to clipboard

setSortby not defined

Open at-webdev opened this issue 6 years ago • 4 comments

I am updating the state of sortBy as below with setSortBy, but I notice error setSortBy not defined on both javascript and typescript environment. Any leads is appreciated.

Codesandbox: https://codesandbox.io/s/restless-firefly-kmoeq?from-embed=&file=/src/App.js

From React-table documentation: setSortBy: Function(sortBy: Array<Object<id: columnId, desc: Bool = true>>) => void This function can be use to programmatically set the sortBy for the table instance

Is it possible to update the state of the sortBy using any other approach.

at-webdev avatar Oct 13 '20 01:10 at-webdev

@at-webdev it's part of the table instance so, you should:

    ...
    rows,
    prepareRow,
    setSortBy, <--- add here
    state: { sortBy }
  } = useTable(

Also, this seems to work for me, no need to wrap in an inner function: setSortBy([{ id: 'firstName', desc: false }])

david-potgieter avatar Oct 16 '20 07:10 david-potgieter

Thanks @david-potgieter, but I am not sure why itsnt working on previous sandbox link. For those wanting to implement setSortby on previous versions useControlledState would fix this.

const { rows, ...., prepareRow, state } = useTable({
  columns: columns,
  data: data,
  initialState: {
    sortBy: [{ id: "age", desc: false }]
  },
  useControlledState: state => {
    return React.useMemo(
      () => ({
        ...state,
        sortBy: [{ id: "firstName", desc: false }] // similar to setSortBy you can set here
      }),
      [state]
    );
  }
});

at-webdev avatar Nov 05 '20 22:11 at-webdev

Also seeing the same issue - setSortBy is undefined on the table instance

gsdatta avatar Jan 06 '21 20:01 gsdatta

This issue is either:

  • Outdated
  • Stale
  • Fixed in v8
  • Unable to be fixed in v7 without breaking changes and is slated for v8

If you feel this is incorrect, please reopen a new issue with a codesandbox reproduction or more info regarding the issue.

tannerlinsley avatar Apr 05 '22 05:04 tannerlinsley