Skip to content

Instantly share code, notes, and snippets.

@coryhouse
Created July 6, 2020 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coryhouse/ba9603ab81af49b1d405fb5a5bd4709e to your computer and use it in GitHub Desktop.
Save coryhouse/ba9603ab81af49b1d405fb5a5bd4709e to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from "react";
export default function InlineDemo() {
const [users, setUsers] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetch(`${process.env.REACT_APP_API_BASE_URL}users`)
.then(response => {
if (response.ok) return response.json();
throw response;
})
.then(json => {
setUsers(json);
})
.catch(err => {
console.error(err);
setError(err);
})
.finally(() => {
setLoading(false);
});
}, []);
if (loading) return "Loading...";
if (error) return "Oops!";
return users[0].username;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment