react-native-google-places-autocomplete给予它一个值,而不仅仅是默认值

n8ghc7c1  于 2023-08-07  发布在  React
关注(0)|答案(4)|浏览(123)

我有一个工作<TextInput>

<TextInput
      placeholder="Location"
      value={props.locationInput.toString()}
      onChangeText={location => props.updateLocationInput(location)}
    />

字符串
props.locationInput最初是'',但一旦应用程序启动,就会触发几个异步函数并获取用户的当前位置,这些位置填充props.locationInput(在redux商店中)。这意味着上面的<TextInput>在到达时显示用户的当前位置。我想基本上就像上面一样,但是使用react-native-google-places-autocomplete
react-native-google-places-autocomplete使用props.locationInput值进行初始化。它有一个getDefaultValue属性,例如getDefaultValue={() => props.locationInput.toString()},但是当props.locationInput改变时,它不会改变,所以它永远不会显示用户的当前位置,因为在初始化时没有设置。如何在props.locationInput更改时更新react-native-google-places-autocomplete
可能我认为我可能需要不渲染它,直到用户的当前位置进来,但这真的很混乱。
编辑:还考虑不使用插件,而是调用Google Places API。

des4xlb0

des4xlb01#

游戏后期,但有一个名为setAddressText的函数。
范例:

setLocation(text) {
  this.placesRef && this.placesRef.setAddressText(text)
}
...
<GooglePlacesAutocomplete
ref={ref => {this.placesRef = ref}}
...
/>

字符串

wqsoz72f

wqsoz72f2#

使用react函数钩子

<GooglePlacesAutocomplete
          ...
          ref={ref => {
            ref?.setAddressText('123 myDefault Street, mycity')
          }}
        />

字符串

zyfwsgd6

zyfwsgd63#

在GooglePlaceAutocomplete组件上,必须使用onPress事件来获取值。下面是一个示例:

<GooglePlacesAutocomplete
                placeholder='Event Location'
                minLength={2} // minimum length of text to search
                autoFocus={false}
                // Can be left out for default return key https://facebook.github.io/react-native/docs/textinput.html#returnkeytype
                listViewDisplayed='auto'    // true/false/undefined
                fetchDetails={true}
                renderDescription={row => row.description} // custom description render
                onPress={(data, details = null) => {
            // 'details' is provided when fetchDetails = true
            this.setState(
              {
                address: data.description, // selected address
                coordinates: `${details.geometry.location.lat},${details.geometry.location.lng}` // selected coordinates
              }
            );
          }}
                textInputProps={{
                  onChangeText: (text) => { console.warn(text) }
                }}
                getDefaultValue={() => ''}

                query={{
                  // available options: https://developers.google.com/places/web-service/autocomplete
                  key: 'XXXXXXXXXXXXXXZXZXXXXXXX',
                  language: 'en', // language of the results
                  types: 'geocode' // default: 'geocode'
                }}

                styles={{
                  textInputContainer: {
                    backgroundColor: 'rgba(0,0,0,0)',
                    borderTopWidth: 0,
                    borderBottomWidth:0,
                  },
                  description: {
                    fontWeight: 'bold',
                  },
                  textInput: {
                  marginLeft: 22,
                  marginRight: 0,
                  height: 38,
                  color: '#5d5d5d',
                  fontSize: 16,
                },
                  predefinedPlacesDescription: {
                    color: '#1faadb'
                  }
                }}
                value={props.location}
                onChangeText={props.onLocationChange}
                renderLeftButton={()  => <Text style={{ marginTop: 12, marginLeft:16, fontSize: 18 }}> Location </Text>}
                nearbyPlacesAPI='GooglePlacesSearch' // Which API to use: GoogleReverseGeocoding or GooglePlacesSearch
                GoogleReverseGeocodingQuery={{
                  // available options for GoogleReverseGeocoding API : https://developers.google.com/maps/documentation/geocoding/intro
                }}
                GooglePlacesSearchQuery={{
                  // available options for GooglePlacesSearch API : https://developers.google.com/places/web-service/search
                  rankby: 'distance',
                  types: 'food'
                }}

                filterReverseGeocodingByTypes={['locality', 'administrative_area_level_3']} // filter the reverse geocoding results by types - ['locality', 'administrative_area_level_3'] if you want to display only cities
                debounce={200} // debounce the requests in ms. Set to 0 to remove debounce. By default 0ms.

              />

字符串

n7taea2i

n7taea2i4#

const ref = useRef<GooglePlacesAutocompleteRef | null>(null);

const handleInputData = () => {
   setTimeout(() => {
        ref?.current?.setAdressTextAndQuery('San Francisco, CA');
    }, 1000);
}

字符串
看看这个:https://github.com/FaridSafi/react-native-google-places-autocomplete/pull/898
ps:这将自动填充输入并动态选择第一个地址以更好地匹配您输入的任何内容。

相关问题