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.
The global fetch
function allows you to easily make 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.
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.
Make sure the fetch()
API and Promises are available in your target audience's browsers.For example, support in Internet Explorer requires a polyfill.
You can learn more about making AJAX requests from React components in the FAQ entry on the React website.