{"id":293491,"date":"2019-07-31T16:03:58","date_gmt":"2019-07-31T23:03:58","guid":{"rendered":"https:\/\/css-tricks.com\/?p=293491"},"modified":"2019-07-31T16:03:58","modified_gmt":"2019-07-31T23:03:58","slug":"fetching-data-in-react-using-react-async","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/fetching-data-in-react-using-react-async\/","title":{"rendered":"Fetching Data in React using React Async"},"content":{"rendered":"
You\u2019re probably used to fetching data<\/a> in React using axios or fetch. The usual method of handling data fetching is to:<\/p>\n There will always be delays when handling requests over the network. That\u2019s just part of the deal when it comes to making a request and waiting for a response. That\u2019s why we often make use of a loading spinner to show the user that the expected response is loading.<\/p>\n \n See the Pen All these can be done using a library called React Async<\/a>.<\/p>\n React Async is a promised-based library that makes it possible for you to fetch data in your React application. Let\u2019s look at various examples using components, hooks and helpers to see how we can implement loading states when making requests.<\/p>\n For this tutorial, we will be making use of Create React App<\/a>. You can create a project by running:<\/p>\n When that is done, run the command to install React Async in your project, using yarn or npm:<\/p>\n The library allows us to make use of First, we created a function called The props are:<\/p>\n As you can see from the example, we return something to be displayed to the user dependent on the prop.<\/p>\n\n
\n ojRMaN<\/a> by Geoff Graham (@geoffgraham<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nnpx create-react-app react-async-demo<\/code><\/pre>\n
## yarn\r\nyarn add react-async\r\n\r\n## npm\r\nnpm install react-async --save<\/code><\/pre>\n
Example 1: Loaders in components<\/h3>\n
<Async><\/code> directly in our JSX<\/abbr>. As such, the component example will look like this;<\/p>\n
\/\/ Let's import React, our styles and React Async\r\nimport React from 'react';\r\nimport '.\/App.css';\r\nimport Async from 'react-async';\r\n\r\n\/\/ We'll request user data from this API\r\nconst loadUsers = () =>\r\n fetch(\"https:\/\/jsonplaceholder.typicode.com\/users\")\r\n .then(res => (res.ok ? res : Promise.reject(res)))\r\n .then(res => res.json())\r\n\r\n\/\/ Our component\r\nfunction App() {\r\n return (\r\n <div className=\"container\">\r\n <Async promiseFn={loadUsers}>\r\n {({ data, err, isLoading }) => {\r\n if (isLoading) return \"Loading...\"\r\n if (err) return `Something went wrong: ${err.message}`\r\n\r\n if (data)\r\n return (\r\n <div>\r\n <div>\r\n <h2>React Async - Random Users<\/h2>\r\n <\/div>\r\n {data.map(user=> (\r\n <div key={user.username} className=\"row\">\r\n <div className=\"col-md-12\">\r\n <p>{user.name}<\/p>\r\n <p>{user.email}<\/p>\r\n <\/div>\r\n <\/div>\r\n ))}\r\n <\/div>\r\n )\r\n }}\r\n <\/Async>\r\n <\/div>\r\n );\r\n}\r\n\r\nexport default App;<\/code><\/pre>\n
loadUsers<\/code>. This will make the API call using the fetch API<\/a>. And, when it does, it returns a promise which gets resolved. After that, the needed props are made available to the component.<\/p>\n
\n
isLoading<\/code>: This handles cases where the response has not be received from the server yet.<\/li>\n
err<\/code>: For cases when an error is encountered. You can also rename this to
error<\/code>.<\/li>\n
data<\/code>: This is the expected data obtained from the server.<\/li>\n<\/ul>\n
Example 2: Loaders in hooks<\/h3>\n