How do I prevent stale search results?
Try code in the playgroundUnderstand the concept
Stale search results occur when requests finish in a different order from the user's inputs. A request for an older search can overwrite newer results unless the component checks which request is still relevant. Debouncing reduces request frequency but does not guarantee completion order.
Important ideas
-
A slow old request can finish after a newer request.
-
Cancel old requests where possible, or ignore results that no longer match the active request.
-
Debounce typing if appropriate, but still handle response ordering.
-
Track the active request or use cleanup to ignore an old result, and handle aborts differently from genuine user-facing failures.
Step-by-step example:
- The user searches for
rea; request A begins. - The user changes the search to
react; request B begins. - B finishes first, so the page shows results for
react. - A finishes later; ignore it because it belongs to an older search.
- Cancel old work when supported, and still protect the state update from stale results.
Can you explain it without the notes?
How do I prevent stale search results?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Stale search results occur when requests finish in a different order from the user's inputs. A request for an older search can overwrite newer results unless the component checks which request is still relevant. Debouncing reduces request frequency but does not guarantee completion order.
Turn this concept into a clear answer.
- Describe the timeline of two searches and show why only the latest relevant response should update state.
Adapted from 06_REACT_NEXTJS.md · “How do I prevent stale search results?”. Examples are study fragments; framework behavior can depend on your version.