TanStackT
TanStackโ€ข3y ago
verbal-lime

How to implement a controlled sorting-state?

Hey ๐Ÿ‘‹๐Ÿฝ ,
if I want control the sorting state of a table v8 in react. Is this the approach to go?
function MyTable({ sorting, data, onSort }) {

  const table = useReactTable({
    data,
    state: { sorting },
    manualSorting: true ,
    columns: tableColumns,
    getCoreRowModel: getCoreRowModel(),
    onSortingChange: (sorting) => {
      if (onSort) {
        onSort(sorting());
      }
    }
  });
   
  useEffect(() => {
    setSorting(sortState);
  }, [sortState]);
}

Thanks!
Was this page helpful?