// We'll request user data from this API const loadUsers = () => fetch('https://jsonplaceholder.typicode.com/users') .then((res) => (res.ok ? res : Promise.reject(res))) .then((res) => res.json());
// Then we'll fetch user data from this API const loadUsers = async () => await fetch('https://jsonplaceholder.typicode.com/users') .then((res) => (res.ok ? res : Promise.reject(res))) .then((res) => res.json());
// Our component functionApp() { const {data, error, isLoading} = useAsync({promiseFn: loadUsers}); if (isLoading) return'Loading...'; if (error) return`Something went wrong: ${error.message}`; if (data) // The rendered component return ( <divclassName="container"> <div> <h2>React Async - Random Users</h2> </div> {data.map((user) => ( <divkey={user.username}className="row"> <divclassName="col-md-12"> <p>{user.name}</p> <p>{user.email}</p> </div> </div> ))} </div> ); }
// This is the API we'll use to request user data const loadUsers = () => fetch('https://jsonplaceholder.typicode.com/users') .then((res) => (res.ok ? res : Promise.reject(res))) .then((res) => res.json());