ErrorPro / react-google-autocomplete

React components for google places API.
MIT License
462 stars 114 forks source link

Is it possible to change country dynamically? #216

Open png-prakash opened 1 year ago

png-prakash commented 1 year ago
import useGoogle from "react-google-autocomplete/lib/usePlacesAutocompleteService";

import Constants from '../../lib/Constants';

const GoogleAddress = (props) => {
  const [value, setValue] = useState("");
  const {placesService, placePredictions, getPlacePredictions} = useGoogle({
    apiKey: Constants.GOOGLE_ADDRESS_API_KEY,
    options: {
      componentRestrictions: { country: props.country },
    },
  });

When change the country in dropdown list is possible to get the addresses based on that particular country?

danielc1015 commented 11 months ago

@png-prakash You have two options:

  1. Lazy load the react component, so the initial value come from the previously selected country.
  2. You separately save in a useState const the options.

const [autocompleteOptions, setAutocompleteOptions] = useState({ componentRestrictions: { country: "cl" }, types: ["address"], })