diff --git a/src/plugin-hooks/useFilters.js b/src/plugin-hooks/useFilters.js index 075f6a9..ce6b971 100755 --- a/src/plugin-hooks/useFilters.js +++ b/src/plugin-hooks/useFilters.js @@ -146,14 +146,14 @@ function useMain(instance) { // Find the filters column const column = columns.find(d => d.id === columnID) - if (depth === 0) { - column.preFilteredRows = filteredSoFar - } - if (!column) { return filteredSoFar } + if (depth === 0) { + column.preFilteredRows = filteredSoFar + } + const filterMethod = getFilterMethod( column.filter, userFilterTypes || {}, diff --git a/src/plugin-hooks/useSortBy.js b/src/plugin-hooks/useSortBy.js index e602a80..3e647ce 100755 --- a/src/plugin-hooks/useSortBy.js +++ b/src/plugin-hooks/useSortBy.js @@ -209,15 +209,28 @@ function useMain(instance) { if (process.env.NODE_ENV === 'development' && debug) console.time('getSortedRows') + // Filter out sortBys that correspond to non existing columns + const availableSortBy = sortBy.filter(sort => + columns.find(col => col.id === sort.id) + ) + const sortData = rows => { // Use the orderByFn to compose multiple sortBy's together. // This will also perform a stable sorting using the row index // if needed. const sortedData = orderByFn( rows, - sortBy.map(sort => { + availableSortBy.map(sort => { // Support custom sorting methods for each column - const { sortType } = columns.find(d => d.id === sort.id) + const column = columns.find(d => d.id === sort.id) + + if (!column) { + throw new Error( + `React-Table: Could not find a column with id: ${sort.id} while sorting` + ) + } + + const { sortType } = column // Look up sortBy functions in this order: // column function @@ -237,11 +250,11 @@ function useMain(instance) { sortMethod(a.values[sort.id], b.values[sort.id], sort.desc) }), // Map the directions - sortBy.map(sort => { + availableSortBy.map(sort => { // Detect and use the sortInverted option - const { sortInverted } = columns.find(d => d.id === sort.id) + const column = columns.find(d => d.id === sort.id) - if (sortInverted) { + if (column && column.sortInverted) { return sort.desc }