Fix the replay of a memoized search filter
Your task
Your app's search list stopped narrowing as users typed. A recent change wrapped the filter in useMemo with the dependency list [rows], but the callback also reads search. useMemo caches a computed value and recomputes it only when a value in its dependency list changes. For the postmortem, the team wants a replay of the row count on screen at each render.
An assistant wrote filtered_counts(rows, searches, deps_include_search). It is in the editor. It has a bug. Find it and fix it.
Input and output:
rows: the row names, as strings. They stay the same on every render.searches: the search box value on each render, in render order.deps_include_search: true when the dependency list is[rows, search], false when it is[rows].- Return a list of integers: the visible row count after each render, in render order.
Rules the tests check:
- A row is visible when the search term appears anywhere inside its name. The check is a plain substring match and is case sensitive. An empty term matches every row.
- Count every visible row, duplicates included.
- The memo always computes on the first render.
- With
searchin the dependency list, the memo recomputes on every render. - Without it, the only dependency never changes. Every later render shows the count from the first render, whatever that count was.
- No searches returns an empty list. No rows gives a count of 0 on every render.
Examples
filtered_counts(rows: list[str], searches: list[str], deps_include_search: bool) -> list[int]Example 1
Input
filtered_counts(["Ada", "Alan", "Grace"], ["", "A", "Ala"], True)Output
[3, 2, 1]Example 2
Input
filtered_counts(["Ada", "Alan", "Grace"], ["", "A", "Ala"], False)Output
[3, 3, 3]Example 3
Input
filtered_counts(["abc"], ["b", "z"], True)Output
[1, 0]