user15322469
user15322469

Reputation: 909

How can i get json in axios with react native?

I'm trying to get json in axios

but if i use my code this error and warning occured

How can i get response.json ??

enter image description here

response.json is not a function

this is my code

      //    url="https://yts.lt/api/v2/list_movies.json?sort_by=like_count&order_by=desc&limit=5"
            
            url is props

     useEffect(() => {
        axios
          .get(url)
          .then((response) => response.json())
          .then((json) => {
            console.log('json', json);
            setData(json.data.movies);
          })
          .catch((error) => {
            console.log(error);
          });
      }, []);

Upvotes: 0

Views: 3709

Answers (2)

Azarro
Azarro

Reputation: 1866

The response object from axios stores its data in response.data.

useEffect(() => {
        axios
          .get(url)
          .then((response) => {
            const json = response.data;
            console.log('json', json);
            setData(json.data.movies);
          })
          .catch((error) => {
            console.log(error);
          });
      }, []);

Upvotes: 2

Tanay
Tanay

Reputation: 889

Use this:

useEffect(() => {
        axios
          .get(url)
          .then((response) => response.data)
          .then((json) => {
            console.log('json', json);
            setData(json.data.movies);
          })
          .catch((error) => {
            console.log(error);
          });
      }, []);

Upvotes: 1

Related Questions