File tree Expand file tree Collapse file tree
react-instantsearch-hooks-native/getting-started/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -8,35 +8,30 @@ type SearchBoxProps = UseSearchBoxProps & {
88
99export function SearchBox ( { onChange, ...props } : SearchBoxProps ) {
1010 const { query, refine } = useSearchBox ( props ) ;
11- const [ value , setValue ] = useState ( query ) ;
11+ const [ inputValue , setInputValue ] = useState ( query ) ;
1212 const inputRef = useRef < TextInput > ( null ) ;
1313
14- // Track when the value coming from the React state changes to synchronize
15- // it with InstantSearch.
16- useEffect ( ( ) => {
17- if ( query !== value ) {
18- refine ( value ) ;
19- }
20- } , [ value , refine ] ) ;
14+ function setQuery ( newQuery : string ) {
15+ setInputValue ( newQuery ) ;
16+ refine ( newQuery ) ;
17+ }
2118
2219 // Track when the InstantSearch query changes to synchronize it with
2320 // the React state.
24- useEffect ( ( ) => {
25- // We bypass the state update if the input is focused to avoid concurrent
26- // updates when typing.
27- if ( ! inputRef . current ?. isFocused ( ) && query !== value ) {
28- setValue ( query ) ;
29- }
30- } , [ query ] ) ;
21+ // We bypass the state update if the input is focused to avoid concurrent
22+ // updates when typing.
23+ if ( query !== inputValue && ! inputRef . current ?. isFocused ( ) ) {
24+ setInputValue ( query ) ;
25+ }
3126
3227 return (
3328 < View style = { styles . container } >
3429 < TextInput
3530 ref = { inputRef }
3631 style = { styles . input }
37- value = { value }
32+ value = { inputValue }
3833 onChangeText = { ( newValue ) => {
39- setValue ( newValue ) ;
34+ setQuery ( newValue ) ;
4035 onChange ( newValue ) ;
4136 } }
4237 clearButtonMode = "while-editing"
You can’t perform that action at this time.
0 commit comments