Created
July 6, 2020 15:05
-
-
Save coryhouse/ba9603ab81af49b1d405fb5a5bd4709e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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