首页 > 解决方案 > 如何从 react-native 中的函数返回数据

问题描述

我正在尝试从函数返回数据,但没有得到正确的数据

export const getApi = (url) => {
    return fetch(url)
        .then((response) => response.json())
        .then((json) => {
            console.log(json);
        })
}
 {"_U": 0, "_V": 0, "_W": null, "_X": null}

这是我的回应

我在这里叫它

 componentDidMount(){
   const data= getApi(banner)       
   console.log('data',data)       
}

标签: javascriptreactjsreact-nativefunction

解决方案


不确定您要在宏伟计划中完成什么,但根据代码片段,您需要这样做:

export const getApi = (url) => {
   return fetch(url)
          .then((response) => response.json())
          .then(json => json)

}

然后像这样使用它:

componentDidMount(){
   getApi(banner).then(data => console.log("data", data))   
}

推荐阅读