Reputation: 181
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)
}
Upvotes: 0
Views: 1155
Reputation: 547
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;
}
Upvotes: 0
Reputation: 7935
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))
}
Upvotes: 1