Skip to content
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

How can I fetch each value and put it into the rest of the inputs I have. They are all using things such as const [address, setAddress] = useState(shippingAddress.address); const [city, setCity] = useState(shippingAddress.city); const [state, setState] = useState(shippingAddress.state) const [postalCode, setPostalCode] = useState(shippingAddress.postalCode); const [country, setCountry] = useState(shippingAddress.country); #371

Open
talmax1124 opened this issue Jul 1, 2021 · 3 comments

Comments

@talmax1124
Copy link

How can I fetch each value and add it to each input which uses state.

Here is an example of the state:
const [address, setAddress] = useState(shippingAddress.address);
const [city, setCity] = useState(shippingAddress.city);
const [state, setState] = useState(shippingAddress.state)
const [postalCode, setPostalCode] = useState(shippingAddress.postalCode);
const [country, setCountry] = useState(shippingAddress.country);

@katherinepamina
Copy link

@talmax1124 how did you end up handling this? I am trying to do the same thing

@davidgs
Copy link

davidgs commented Jan 1, 2023

I'm doing the following:

const handleSelect = (value) => {
    console.log('handleSelect: ', value)
    const comps = value.split(','); // return has form `address, City, State, Country`
    console.log('comps: ', comps);
    setAddress(comps[0]);
    setCity(comps[1]);
    setCountry(comps[2]);
    });

But it's not returning a zip code/postal code, so I have to figure that out.

@twoelevenjay
Copy link

@davidgs @talmax1124 @katherinepamina

I used the build in geocodeByAddress() method, and then made a function that gets the address_component I want by type.

  const getComponent = (address_components, type) => {
    return address_components?.find((component) =>
      component.types.includes(type)
    );
  };
  const handleChange = (address) => {
    let street = address.split(", ")[0];
    setAddress(street);
    setValue(name, street);
    geocodeByAddress(address)
      .then((results) => {
        console.log(results);
        let city = getComponent(
          results[0].address_components,
          "administrative_area_level_3"
        );
        if (typeof city === "undefined") {
          city = getComponent(results[0].address_components, "locality");
        }
        if (typeof city !== "undefined") {
          setValue("city", city.long_name);
        }
        let state = getComponent(
          results[0].address_components,
          "administrative_area_level_1"
        );
        if (typeof city !== "undefined") {
          setValue("state", state.long_name);
        }
        let zipcode = getComponent(
          results[0].address_components,
          "postal_code"
        );
        if (typeof zipcode !== "undefined") {
          setValue("zipcode", zipcode.long_name);
        }
      })
      .catch((error) => {
        console.error("ErrorXXX", error);
        setValue("city", "");
        setValue("state", "");
        setValue("zipcode", "");
      });
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants