I want to be able to populate the search bar with the previous search which under normal circumstances happens automatically but with my current setup I have to do it using a function.
I have put the search bar in a modal. So I want to pass props to the modal component so that after a search and an onPress event if the modal with the search bar is opened again I can prepopulate it with the previous search text. According to the documentation, there is a "setAddressText" method which should help do what I am trying to achieve. But I am not quite sure how I can use the method in this component and there's is no descriptive example.
Steps to reproduce the behavior - a minimal reproducible code example, link to a snack or a repository.
So as you know once a modal is closed, it's unmounted from the app, the values reset. I have passed props to the modal and when I log the props to the console they are there but I haven't quite figured put how to pass a value to the search bar on the condition that a previous search was made. I hope I am making sense with what I am explaining.
"react-native-google-places-autocomplete": "^1.8.0"
-React Native Version: [sdk-38.0.2]
If you are using expo please indicate here:
Add any other context about the problem here, screenshots etc
Have you tried the getDefaultValue prop yet?
<GooglePlacesAutocomplete
placeholder="Search"
onPress={(data, details) => {
console.log(data, details);
}}
query={{
key: GOOGLE_PLACES_API_KEY,
language: 'en',
}}
getDefaultValue={() => 'Previous Address'}
/>
Alternatively, you can use a ref and use setAddressText in componentDidMount or a useEffect.
import React, { useRef, useEffect } from 'react';
import { StyleSheet, View } from 'react-native';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
const GOOGLE_PLACES_API_KEY = '';
const App = () => {
const ref = useRef();
useEffect(() => {
ref?.current?.setAddressText('Previous Address');
}, []);
return (
<View style={styles.container}>
<GooglePlacesAutocomplete
ref={ref}
placeholder="Search"
onPress={(data, details) => {
console.log(data, details);
}}
query={{
key: GOOGLE_PLACES_API_KEY,
language: 'en',
}}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 10,
backgroundColor: '#ecf0f1',
padding: 8,
},
});
export default App;
Let me know what worked for you.
@bell-steven your post is no longer relevant as you removed the getDefaultValue prop.
What is the new way to do this?
@tgreco use a ref like my second example above. The changelog mentions this change.
@tgreco use a ref like my second example above. The changelog mentions this change.
The second example is not working for me.
componentDidMount()
{
this._placeRef.current.setAddressText('Test');
console.log('Set address');
this.forceUpdate();
}
Nothing appears in the search text field at all.
@bell-steven
Please follow the example in the README. I just tested it, and it definitely works.
@bell-steven have you tried this with a component using componentDidMount instead of useEffect?
It does not seem to work for me unless using a functional component.
I was using useEffect. You are probably not accessing the ref correctly in your componentDidMount
Has anyone been able to use setAddressText in componentDidMount?
@bell-steven can you please give an example how to use correctly in componentDidMount?
Has anyone been able to use
setAddressTextin componentDidMount?
@bell-steven can you please give an example how to use correctly in componentDidMount?
Unfortunately this will only work in a functional component.
A class it will not work.
Most helpful comment
Have you tried the
getDefaultValueprop yet?Alternatively, you can use a ref and use
setAddressTextincomponentDidMountor auseEffect.Let me know what worked for you.