From dda6bbfb9583555098996ae5b09c934b632d9a7b Mon Sep 17 00:00:00 2001 From: Corbin Robb Date: Fri, 11 Feb 2022 13:08:57 -0700 Subject: [PATCH] clean up setState --- .../FilterableTable/FilterableTable.tsx | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/superset-frontend/src/components/FilterableTable/FilterableTable.tsx b/superset-frontend/src/components/FilterableTable/FilterableTable.tsx index 89f8080cc8ca1..23d68f96850c9 100644 --- a/superset-frontend/src/components/FilterableTable/FilterableTable.tsx +++ b/superset-frontend/src/components/FilterableTable/FilterableTable.tsx @@ -296,30 +296,31 @@ export default class FilterableTable extends PureComponent< sortBy: string; sortDirection: SortDirectionType; }) { - this.setState( - ({ - sortBy: prevSortBy, - sortDirection: prevSortDirection, - displayedList: prevDisplayedList, - }) => { - const shouldClearSort = - prevSortDirection === SortDirection.DESC && prevSortBy === sortBy; - if (shouldClearSort) - return { - sortBy: undefined, - sortDirection: undefined, - displayedList: [...this.list], - }; - - return { - sortBy, - sortDirection, - displayedList: prevDisplayedList.sort( - this.sortResults(sortBy, sortDirection === SortDirection.DESC), - ), - }; - }, - ); + let updatedState: FilterableTableState; + + const shouldClearSort = + this.state.sortDirection === SortDirection.DESC && + this.state.sortBy === sortBy; + + if (shouldClearSort) { + updatedState = { + ...this.state, + sortBy: undefined, + sortDirection: undefined, + displayedList: [...this.list], + }; + } else { + updatedState = { + ...this.state, + sortBy, + sortDirection, + displayedList: [...this.list].sort( + this.sortResults(sortBy, sortDirection === SortDirection.DESC), + ), + }; + } + + this.setState(updatedState); } fitTableToWidthIfNeeded() {