Fix stale searchText in search submit handler

The onSubmit handler captured searchText from the render closure, so if
the user typed fast and hit enter before React committed the last
keystroke's re-render, the search would use a stale value. Use a ref
instead so the handler always reads the latest searchText at call time.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Eric Bailey
2026-04-14 11:21:20 -05:00
parent c56427c6fb
commit 4eb9b36e3d
+8 -9
View File
@@ -97,6 +97,8 @@ export function SearchScreenShell({
// Query terms
const [searchText, setSearchText] = useState<string>(queryParam)
const searchTextRef = useRef(searchText)
searchTextRef.current = searchText
const {data: autocompleteData, isFetching: isAutocompleteFetching} =
useActorAutocompleteQuery(searchText, true)
@@ -227,15 +229,12 @@ export function SearchScreenShell({
}
}, [setShowAutocomplete, setSearchText, navigation, route.params, route.name])
const onSubmit = useCallback(
(source: 'typed' | 'autocomplete') => () => {
ax.metric('search:query', {
source,
})
navigateToItem(searchText)
},
[ax, navigateToItem, searchText],
)
const onSubmit = (source: 'typed' | 'autocomplete') => () => {
ax.metric('search:query', {
source,
})
navigateToItem(searchTextRef.current)
}
const onAutocompleteResultPress = useCallback(() => {
if (IS_WEB) {