Data fetching
In the preceding section, we saw the different patterns we can put in place to share data between components in the tree.
It is now time to view how to fetch data in React and where the data fetching logic should be located.
The examples in this section use the fetch function to make web requests, which is a modern replacement for XMLHttpRequest
.
At the time of writing, it is natively implemented in Chrome and FireFox and if you need to support different browsers, you must use the fetch polyfill by GitHub:
https://github.com/github/fetch
We are also going to use the public GitHub APIs to load some data and the endpoint we will use is the one that returns a list of gists, given a username:
https://api.github.com/users/:username/gists
Gists are snippets of code that can be easily shared between developers.
The first component that we will build is a simple list of the gists created by the user, called gaearon (Dan Abramov).
Let's delve into some code and create a class.
We use a class...