I'm trying to return data from a function but didn't get proper data
export const getApi = (url) => {
return fetch(url)
.then((response) => response.json())
.then((json) => {
console.log(json);
})
}
{"_U": 0, "_V": 0, "_W": null, "_X": null}
this is my response
i'm calling it here
componentDidMount(){
const data= getApi(banner)
console.log('data',data)
}
Unsure precisely what you're trying to accomplish in the grand scheme, but based on the code snippet, you need to do it this way:
export const getApi = (url) => {
return fetch(url)
.then((response) => response.json())
.then(json => json)
}
Then use it like so:
componentDidMount(){
getApi(banner).then(data => console.log("data", data))
}
You can rewrite your function to this. It is also more readable.
export const getApi = async (url) => {
const response = await fetch(url);
const json = await response.json();
console.log(json);
return json;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.