Skip to content

Commit fd3420a

Browse files
chore: add comment to explain
1 parent 25db559 commit fd3420a

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

src/hooks/useSearchResults.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ function useSearchResults<TValue extends ListItem>(data: TValue[], filterData: (
1616
useEffect(() => {
1717
startTransition(() => {
1818
const normalizedSearchQuery = inputValue.trim().toLowerCase();
19+
20+
// Create shallow copy of data to prevent mutation. When no search query exists, we pass the full dataset
21+
// to sortData. If sortData uses Array.sort() (which sorts in place and returns the same reference),
22+
// the original data array would be mutated. This breaks React's reference equality check in setResult,
23+
// preventing re-renders even when the sort order changes (e.g., on page refresh).
1924
const filtered = normalizedSearchQuery.length ? data.filter((item) => filterData(item, normalizedSearchQuery)) : [...data];
2025
const sorted = sortData(filtered);
2126
setResult(sorted);

0 commit comments

Comments
 (0)