-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starts using react useMemo, so It only calculates the filteredAuthors if either the filter or the fetched data changes. Also, it is now filtering before mapping which also improves the performance
- Loading branch information
1 parent
df2065c
commit 8d408da
Showing
3 changed files
with
34 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,17 @@ | ||
import { ChangeEvent, useState } from "react"; | ||
import { useState } from "react"; | ||
|
||
export const useFilterHook = () => { | ||
const [filter, setFilter] = useState<string | null>(null); | ||
|
||
const handleChangeFilter = (e: ChangeEvent<HTMLInputElement>): void => { | ||
setFilter(e.target.value); | ||
}; | ||
|
||
const shouldFilterInWith = (...properties: (unknown | undefined)[]): boolean => { | ||
const isFilterFoundInProperties = (...properties: (unknown | undefined)[]): boolean => { | ||
if (!filter) return true; | ||
|
||
let anyPropertyMatch = false; | ||
|
||
for (const property of properties) { | ||
if (typeof property === "undefined") { | ||
continue; | ||
} | ||
|
||
if (String(property).toLowerCase().includes(filter.toLowerCase())) { | ||
anyPropertyMatch = true; | ||
break; | ||
} | ||
} | ||
|
||
return anyPropertyMatch; | ||
return properties.some((property) => property && String(property).toLowerCase().includes(filter.toLowerCase())); | ||
}; | ||
|
||
return { | ||
handleChangeFilter, | ||
shouldFilterInWith, | ||
filter, | ||
setFilter, | ||
isFilterFoundInProperties, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters