Fetching Data with AJAX Requests
React doesn’t prescribe a specific approach to data fetching, but people commonly use either a library like axios or the fetch() API provided by the browser. Conveniently, Create React App includes a polyfill for fetch()
so you can use it without worrying about the browser support.
The global fetch
function allows to easily makes AJAX requests. It takes in a URL as an input and returns a Promise that resolves to a Response object. You can find more information about fetch
here.
This project also includes a Promise polyfill which provides a full implementation of Promises/A+. A Promise represents the eventual result of an asynchronous operation, you can find more information about Promises here and here. Both axios
and fetch()
use Promises under the hood. You can also use the async/await syntax to reduce the callback nesting.
You can learn more about making AJAX requests from React components in next chapter.