-
-
Notifications
You must be signed in to change notification settings - Fork 855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting Predefined Text #609
Comments
Have you tried the <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 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? |
The second example is not working for me.
Nothing appears in the search text field at all. |
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? |
I was using useEffect. You are probably not accessing the ref correctly in your componentDidMount |
Has anyone been able to use |
Unfortunately this will only work in a functional component. |
Hello, is it planned to make it work in a class component? Indeed I struggled a long time before reading that it can't work in a class component. Unfortunately my GooglePlacesAutocomplete component is inside a long list of other components and I cannot imagine turning all of it into a functional component... |
What a terrible choice to remove getDefaultValue and not provide any alternatives!!! For anyone who stumbles upon this, here's my solution: const [stepAddress, setStepAddress] = useState("Your Value Here");
const [allowEmpty, setAllowEmpty] = useState(false);
function handleTT(ipn){
if (ipn.length > 0 || allowEmpty) {
setStepAddress(ipn);
} else {
setAllowEmpty(true);
}
} And then add this to your autocomplete component: textInputProps={{
value: stepAddress,
onChangeText: handleTT
}} What a mess! Not only does this library not provide any way to set the default text, but it also tries to override whatever you put in and set it to an empty string If anyone else has a better solution let me know. Yikes! |
I was able to // GooglePlacesAutocomplete wrapper component
import { Component, forwardRef } from 'react'
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete'
class GooglePlacesAutocompleteWrapper extends Component {
shouldComponentUpdate() {
const { innerRef, defaultLocation } = this.props
if (defaultLocation) {
innerRef.current.setAddressText(`${defaultLocation.city}, ${defaultLocation.state}`)
return true
}
return false
}
render() {
return (
<GooglePlacesAutocomplete
ref={this.props.innerRef}
{/* the props you need/want */}
/>
)
}
}
// forwarding wrapper ref to its parent
export const MyGooglePlacesAutocompleteComponent = forwardRef((props, ref) => {
return <GooglePlacesAutocompleteWrapper {...props} innerRef={ref} />
})
// Parent component
import { createRef } from 'react'
import { MyGooglePlacesAutocompleteComponent } from '@components/MyGooglePlacesAutocompleteComponent'
export class ParentComponent extends Component {
googlePlacesInputRef = createRef()
render() {
<MyGooglePlacesAutocompleteComponent
ref={this.googlePlacesInputRef}
defaultLocation={{
city: 'New York',
state: 'NY',
}}
/>
}
} |
Describe the problem
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.
Reproduction - (required - issue will be closed without this)
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.
Additional context
"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
The text was updated successfully, but these errors were encountered: