0

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)       
}
1

2 Answers 2

1

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))   
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks , I got it now fixed my problem
@AlokSingh You're welcome. Don't forget to accept the answer if it addressed your problem.
0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.