fix: add debounced to podgroup search#285
Conversation
Signed-off-by: pggdev <princegupta.ns153@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Code Review
This pull request introduces a debounced search mechanism for pod group management to reduce redundant API queries. The feedback suggests optimizing the useEffect hook with a guard clause to prevent unnecessary timeouts on initial mount and when clearing the search term.
| useEffect(() => { | ||
| const timer = window.setTimeout(() => { | ||
| setDebouncedSearchTerm(searchTerm) | ||
| setPagination((prev) => ({ ...prev, page: 1 })) | ||
| }, 200) | ||
|
|
||
| return () => window.clearTimeout(timer) | ||
| }, [searchTerm]) |
There was a problem hiding this comment.
The useEffect hook currently runs on every change of searchTerm, including the initial mount and when the search is cleared (where both searchTerm and debouncedSearchTerm are immediately set to ""). This results in redundant timeouts and state updates.
By adding a guard clause if (searchTerm === debouncedSearchTerm) return; and including debouncedSearchTerm in the dependency array, we can prevent unnecessary timeouts on mount, on clear, and after the debounce successfully completes.
useEffect(() => {
if (searchTerm === debouncedSearchTerm) return;
const timer = window.setTimeout(() => {
setDebouncedSearchTerm(searchTerm)
setPagination((prev) => ({ ...prev, page: 1 }))
}, 200)
return () => window.clearTimeout(timer)
}, [searchTerm, debouncedSearchTerm])
Summary
Testing