首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在返回函数之前,useEffect没有运行

在返回函数之前,useEffect没有运行
EN

Stack Overflow用户
提问于 2021-05-04 01:44:57
回答 1查看 33关注 0票数 0

我已经检索到用户的位置,并希望显示有关用户的县、州/省和国家的信息。我使用Google Maps API检索县、州和国家,并将它们存储为变量。然而,当我启动我的expo应用程序时,这些数据并没有加载。我必须保存我的项目,然后它将运行所有内容。然后,将显示位置名称。我如何才能让它在应用程序运行时显示位置,而不是每次都保存项目?我是否正确地使用了useEffect

代码语言:javascript
运行
复制
  const [statedata, setstateData] = useState(null);
  var [countydata, setcountydata] = useState(null);
  const [countrydata, setcountrydata] = useState(null);
  var [stateNameshort, setstateNameshort] = useState(String);
  var [countryNameshort, setCountryNameshort] = useState(String);

  useEffect(() => {
    fetch(
      "https://maps.googleapis.com/maps/api/geocode/json?address=" +
        latitude +
        "," +
        longitude +
        "&key=" +
        apiKey
    )
      .then((response) => response.json())
      .then((responseJson) => {
        const resState = responseJson.results[0].address_components.filter(
          (x: any) =>
            x.types.filter((t: Object) => t == "administrative_area_level_1")
              .length > 0
        )[0].long_name;
        setstateData(resState);
        const resCounty = responseJson.results[0].address_components.filter(
          (x: any) =>
            x.types.filter((t: Object) => t == "administrative_area_level_2")
              .length > 0
        )[0].long_name;
        setcountydata(resCounty);
        const resCountry = responseJson.results[0].address_components.filter(
          (x: any) => x.types.filter((t: Object) => t == "country").length > 0
        )[0].long_name;
        setcountrydata(resCountry);
        const resStateShort = responseJson.results[0].address_components.filter(
          (x: any) =>
            x.types.filter((t: Object) => t == "administrative_area_level_1")
              .length > 0
        )[0].short_name;
        setstateNameshort(resStateShort);
        const resCountryShort = responseJson.results[0].address_components.filter(
          (x: any) => x.types.filter((t: Object) => t == "country").length > 0
        )[0].short_name;
        setCountryNameshort(resCountryShort);
        if (countryNameshort === "US") {
          countryNameshort = "US" + "A";
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }, []);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-04 01:56:59

实现它,就像这样

代码语言:javascript
运行
复制
useEffect(() => {
  const unsubscribe = navigation.addListener("focus", () => {
    GetLocation() // Gets fired whenever this screen is in focus
  });

  return unsubscribe;
}, [navigation]);

const GetLocation = async () => { // Gets Location
  fetch(
    "https://maps.googleapis.com/maps/api/geocode/json?address=" +
      latitude +
      "," +
      longitude +
      "&key=" +
      apiKey
  )
    .then((response) => response.json())
    .then((responseJson) => {
      const resState = responseJson.results[0].address_components.filter(
        (x: any) =>
          x.types.filter((t: Object) => t == "administrative_area_level_1")
            .length > 0
      )[0].long_name;
      setstateData(resState);
      const resCounty = responseJson.results[0].address_components.filter(
        (x: any) =>
          x.types.filter((t: Object) => t == "administrative_area_level_2")
            .length > 0
      )[0].long_name;
      setcountydata(resCounty);
      const resCountry = responseJson.results[0].address_components.filter(
        (x: any) => x.types.filter((t: Object) => t == "country").length > 0
      )[0].long_name;
      setcountrydata(resCountry);
      const resStateShort = responseJson.results[0].address_components.filter(
        (x: any) =>
          x.types.filter((t: Object) => t == "administrative_area_level_1")
            .length > 0
      )[0].short_name;
      setstateNameshort(resStateShort);
      const resCountryShort = responseJson.results[0].address_components.filter(
        (x: any) => x.types.filter((t: Object) => t == "country").length > 0
      )[0].short_name;
      setCountryNameshort(resCountryShort);
      if (countryNameshort === "US") {
        countryNameshort = "US" + "A";
      }
    })
    .catch((err) => {
      console.log(err);
    });
};

此外,我更喜欢将我的fetch操作编写为async/await。但这取决于你,你想写什么就写哪一个

async/await类型

代码语言:javascript
运行
复制
const GetLocation = async () => {
  const response = await fetch(
    "https://maps.googleapis.com/maps/api/geocode/json?address=" +
      latitude +
      "," +
      longitude +
      "&key=" +
      apiKey
  );

  const result = await response.json();
  
  const resState = responseJson.results[0].address_components.filter(
    (x: any) =>
      x.types.filter((t: Object) => t == "administrative_area_level_1").length >
      0
  )[0].long_name;
  setstateData(resState);
  const resCounty = responseJson.results[0].address_components.filter(
    (x: any) =>
      x.types.filter((t: Object) => t == "administrative_area_level_2").length >
      0
  )[0].long_name;
  setcountydata(resCounty);
  const resCountry = responseJson.results[0].address_components.filter(
    (x: any) => x.types.filter((t: Object) => t == "country").length > 0
  )[0].long_name;
  setcountrydata(resCountry);
  const resStateShort = responseJson.results[0].address_components.filter(
    (x: any) =>
      x.types.filter((t: Object) => t == "administrative_area_level_1").length >
      0
  )[0].short_name;
  setstateNameshort(resStateShort);
  const resCountryShort = responseJson.results[0].address_components.filter(
    (x: any) => x.types.filter((t: Object) => t == "country").length > 0
  )[0].short_name;
  setCountryNameshort(resCountryShort);
  if (countryNameshort === "US") {
    countryNameshort = "US" + "A";
  }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67373447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档