From 253212a77ab834582c21d1abed47e93fa97697ec Mon Sep 17 00:00:00 2001 From: Hailey Date: Wed, 24 Apr 2024 11:49:50 -0700 Subject: [PATCH] use `useFocusEffect` to update the query term on back navigation --- src/view/screens/Search/Search.tsx | 33 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/view/screens/Search/Search.tsx b/src/view/screens/Search/Search.tsx index 7efb70895e..3ace46f991 100644 --- a/src/view/screens/Search/Search.tsx +++ b/src/view/screens/Search/Search.tsx @@ -39,6 +39,7 @@ import { import {useSession} from '#/state/session' import {useSetDrawerOpen} from '#/state/shell' import {useSetDrawerSwipeDisabled, useSetMinimalShellMode} from '#/state/shell' +import {useNonReactiveCallback} from 'lib/hooks/useNonReactiveCallback' import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries' import { NativeStackScreenProps, @@ -543,7 +544,6 @@ export function SearchScreen( // Query terms const q = props.route?.params?.q ?? '' - const [query, setQuery] = React.useState(q) const [searchText, setSearchText] = React.useState(q) const throttledInput = useThrottledValue(searchText, 300) @@ -555,10 +555,11 @@ export function SearchScreen( const [inputIsFocused, setInputIsFocused] = React.useState(false) const [searchHistory, setSearchHistory] = React.useState([]) - if (q !== query) { - setQuery(q) - setSearchText(q) - } + useFocusEffect( + useNonReactiveCallback(() => { + setSearchText(q) + }), + ) React.useEffect(() => { const loadSearchHistory = async () => { @@ -600,16 +601,16 @@ export function SearchScreen( scrollToTopWeb() if (inputIsFocused) { - setSearchText(query) + setSearchText(q) textInput.current?.blur() } else { - if (isWeb && query) { + if (isWeb && q) { navigation.goBack() } else { navigation.setParams({q: ''}) } } - }, [inputIsFocused, navigation, query]) + }, [inputIsFocused, navigation, q]) const onChangeText = React.useCallback(async (text: string) => { scrollToTopWeb() @@ -643,7 +644,6 @@ export function SearchScreen( const onSubmit = React.useCallback(() => { scrollToTopWeb() updateSearchHistory(searchText) - setQuery(searchText) if (isWeb) { navigation.push('Search', {q: searchText}) @@ -658,9 +658,9 @@ export function SearchScreen( }, [onPressCancelSearch]) const queryMaybeHandle = React.useMemo(() => { - const match = MATCH_HANDLE.exec(query) + const match = MATCH_HANDLE.exec(q) return match && match[1] - }, [query]) + }, [q]) useFocusEffect( React.useCallback(() => { @@ -669,9 +669,8 @@ export function SearchScreen( }, [onSoftReset, setMinimalShellMode]), ) - const handleHistoryItemClick = (item: React.SetStateAction) => { - setQuery(item) - onSubmit() + const handleHistoryItemClick = (item: string) => { + navigation.setParams({q: item}) } const handleRemoveHistoryItem = (itemToRemove: string) => { @@ -768,7 +767,7 @@ export function SearchScreen( ) : undefined} - {(query || inputIsFocused) && ( + {(q || inputIsFocused) && ( )} - ) : !query && inputIsFocused ? ( + ) : !q && inputIsFocused ? ( ) : ( - + )} )