Closed badhon252 closed 5 days ago
Fix: Prevent infinite loop in search functionality
Implemented debounce mechanism in components/Navbar/SearchBar.tsx to delay search updates. Memoized search results in hooks/useSearchNews.tsx to optimize re-renders. Resolved "Maximum update depth exceeded" error.
The application is experiencing a "Maximum update depth exceeded" error in the search functionality. This error occurs when the user enters a search query in the SearchBar component. It is triggered by a recursive loop involving the setSearchQuery and setSearchResults functions in store/useNewsStore.ts.
Steps to Reproduce:
Expected Behavior:
The search functionality should filter the news articles based on the user's query and display the results in the SearchResultsModal component.
Possible Solutions:
Introduce a debounce mechanism to delay the search update and prevent the infinite loop. Memoize the search results to prevent unnecessary re-renders when the search query remains unchanged.
Additional Information:
The relevant code snippets are in store/useNewsStore.ts, hooks/useSearchNews.tsx, and components/Navbar/SearchBar.tsx.