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 ...

Get React Design Patterns and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.