Dylan Steck Dylan Steck - 28 days ago 24
React JSX Question

How to query JSON data from REST API in React Native

I want to query through some data from NewsAPI in React Native and have no idea how to do so and none of the tutorials I've seen have helped. Thanks!

Answer

Check out the Docs on Networking from React Native... I'm using Fetch in our production apps.

React Native provides the Fetch API for your networking needs. Fetch will seem familiar if you have used XMLHttpRequest or other networking APIs before. You may refer to MDN's guide on Using Fetch for additional information.

MDN Fetch API Guide

fetch('http://google.com')
 .then(res => {
    if (res.ok) return res.json())
    else throw new Error(res)
 .then(json => {
   for (var key in json) {
     // now you can parse it by calling json[key]
   }
 .catch(error => console.log(error)
 }
Comments