Skip to content

Commit

Permalink
#149 - Optimise useSortableDataTable logic with useMemo
Browse files Browse the repository at this point in the history
  • Loading branch information
imikulec committed Jan 24, 2024
1 parent 821537a commit 28a3b8d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions libs/data-display/src/useSortableDataTable.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useMemo } from "react";

import { useDataTable } from "./index";

export default function useSortableDataTable<T, U extends keyof T>(initialData: T[], columnMapping: Record<U, string>) {
const [dataTableState, dataTableHook] = useDataTable();

const generateSortedData = () => {
const generateSortedData = useMemo(() => {
const sortInstructions = dataTableState.sortBy;

if (!sortInstructions || sortInstructions.length === 0) {
Expand All @@ -23,8 +25,10 @@ export default function useSortableDataTable<T, U extends keyof T>(initialData:
}

return 0;
})
};
});
}, [dataTableState.sortBy]);

return { sortedData: generateSortedData(), dataTableState, dataTableHook };
return useMemo(() => {
return { sortedData: generateSortedData, dataTableState, dataTableHook };
}, [generateSortedData]);
}

0 comments on commit 28a3b8d

Please sign in to comment.