使用获取对象中的日期创建链接以获取React中的另一个对象

亚历山德拉·米洛夫

我写了一些代码和作品,但我认为也许可以用更好的方法来完成。我想从代码中得到什么?我创建链接并从该对象中获取对象,我想使用某个值,并在获取新对象后在另一个链接中传递该值。我的代码正常工作,但我想看看是否有可能采用新的解决方案。

const [key, setKey] = useState("");
const [data, setData] = useState([]);

useEffect(() => { 
    getKey();
    getWeather();
},[key]);

//此函数从对象获取密钥,而该密钥我将在另一个链接中使用

const getKey = () => {
    navigator.geolocation.getCurrentPosition(

        (position) => {
            const long = JSON.stringify(position.coords.longitude);
            const lat = JSON.stringify(position.coords.latitude);

            const proxy = `https://cors-anywhere.herokuapp.com/`;
            const link = `${proxy}http://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=rhlYEhvAu0nhFNMFybOIhffbmjFX0AZN&q=${lat}%2C${long}&details=true`;

            (async function fetchData(){
                const getValue = await fetch (link);
                const key = await getValue.json();
                setKey(key.Key);
            })();
        }
     );
};

const getWeather = async () => {
    const proxy = `https://cors-anywhere.herokuapp.com/`;
    const link = `${proxy}http://dataservice.accuweather.com/forecasts/v1/daily/5day/${key}?apikey=rhlYEhvAu0nhFNMFybOIhffbmjFX0AZN&details=true&metric=true`;
    const data = await fetch (link);
    const getData = await data.json();
    setData(getData);
};
从技术上来说尼克

您只需对代码做一些微小的更改就可以完成这项工作。制作useEffectasync函数,将键从返回getKey到变量,然后等待变量分配并传递给getWeather像这样:

const [key, setKey] = useState("");
const [data, setData] = useState([]);

useEffect(async() => { // <---- Converted to async
    const apiKey = getKey(); // <---- Assigned to variable
    getWeather(await apiKey); // <--- Using apiKey in function rather than just state
},[key]);

const getKey = () => {
    navigator.geolocation.getCurrentPosition(

        (position) => {
            const long = JSON.stringify(position.coords.longitude);
            const lat = JSON.stringify(position.coords.latitude);

            const proxy = `https://cors-anywhere.herokuapp.com/`;
            const link = `${proxy}http://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=rhlYEhvAu0nhFNMFybOIhffbmjFX0AZN&q=${lat}%2C${long}&details=true`;

            (async function fetchData(){
                const getValue = await fetch (link);
                const key = await getValue.json();
                setKey(key.Key);
                return key.Key     //<------ returned key for useEffect
            })();
        }
     );
};

const getWeather = async (apiKey = key) => { // <----If no value passed to function, will use state value
    const proxy = `https://cors-anywhere.herokuapp.com/`;
    const link = `${proxy}http://dataservice.accuweather.com/forecasts/v1/daily/5day/${apiKey}?apikey=rhlYEhvAu0nhFNMFybOIhffbmjFX0AZN&details=true&metric=true`;
    const data = await fetch (link);
    const getData = await data.json();
    setData(getData);
};

我返回该值而不是使用state的原因是因为设置状态是异步的,并且useState设置函数目前没有像for那样的回调函数setState

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在React中,如何使用另一个属性值获取对象的属性值?

获取对象列表中的最高日期,并获取其类的另一个属性

如果对象的名称与另一个相同,则 React js 从对象中获取子项

在另一个表中获取Nil的对象

获取另一个类中对象的值

React获取另一个对象内的对象

如何获取我在另一个对象中创建的变量

如何在另一个对象中获取一个对象?

如何使用 stdClass 对象从另一个数组中的数组中获取值?

如果对象与另一个对象属性匹配,如何获取对象中的值

如何使用 Axios 在另一个对象中获取数组?

如何通过Django中的另一个ForeignKey使用ForeignKey字段获取对象?

使用方法中传递的另一个参数获取对象的参数 - Java

使用另一个数组从对象数组中获取数据

如何通过比较值从嵌套对象数组中获取信息以创建另一个嵌套对象?

在另一个对象中创建的模拟对象

在另一个对象中动态创建对象

在lamba表达式中的另一个对象列表中获取对象列表

无法从另一个对象中获取对象中的值(java处理)

尝试根据nodejs中的另一个集合对象从集合中获取对象

如何从另一个可选对象中的可选对象中获取值?

从 SwiftUI 中的嵌套 JSON API 中获取数据(另一个对象中的对象中的数组中的对象)

如何从一个块中获取对象并在另一个类或方法中使用它?

在Aspect中获取目标对象并在目标对象上调用另一个方法

如何在C#中获取另一个对象的属性的对象类型?

如何从 Django 中另一个对象列表的查询集中获取对象

获取另一个jquery对象中jquery对象的索引

如何在JQuery中的另一个对象内获取对象

如何获取嵌套在另一个对象中的对象的已知属性?