perf(blog): derive search results with useMemo instead of useState+us…#1322
Conversation
…eEffect Fixes MODSetter#1246 Replace the useState/useEffect pattern that synced fuzzy search results into local state on every search or searcher change with a single useMemo that derives results directly during render. Before: const [results, setResults] = useState(allBlogs); useEffect(() => { setResults(searcher.search(search)); }, [search, searcher]); After: const gridItems = useMemo(() => { const results = search.trim() ? searcher.search(search) : allBlogs; ... }, [search, searcher, allBlogs, featuredSlug]); This removes an extra re-render per keystroke and eliminates the stale intermediate state that occurred between the search input change and the effect firing.
|
@guangyang1206 is attempting to deploy a commit to the Rohan Verma's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…eEffect
Fixes #1246
Replace the useState/useEffect pattern that synced fuzzy search results into local state on every search or searcher change with a single useMemo that derives results directly during render.
Before:
const [results, setResults] = useState(allBlogs);
useEffect(() => {
setResults(searcher.search(search));
}, [search, searcher]);
After:
const gridItems = useMemo(() => {
const results = search.trim() ? searcher.search(search) : allBlogs;
...
}, [search, searcher, allBlogs, featuredSlug]);
This removes an extra re-render per keystroke and eliminates the stale intermediate state that occurred between the search input change and the effect firing.
Description
Closes #1246
Replace the
useState+useEffectpattern inMagazineSearchGridthat synced fuzzy search results into local state on everysearch/searcherchange with a singleuseMemothat derives filtered results directly during render.Before:
After:
Benefits
results,setResults) and theuseEffectHow Has This Been Tested?
Manually tested in the blog search input — search results filter correctly as you type, with no visible flash of stale results.
Type of Change
Checklist
High-level PR Summary
This PR optimizes the blog search functionality by replacing a
useState+useEffectpattern with a singleuseMemohook to derive search results during render. This eliminates an unnecessary re-render on each keystroke and removes the stale intermediate state that occurred between search input changes and the effect firing, resulting in a more efficient and simpler component implementation.⏱️ Estimated Review Time: 5-15 minutes
💡 Review Order Suggestion
surfsense_web/app/(home)/blog/blog-magazine.tsx