Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created July 3, 2019 16:09
Show Gist options
  • Save jsmanifest/64d1e8c551869643ef2a555d7dd79671 to your computer and use it in GitHub Desktop.
Save jsmanifest/64d1e8c551869643ef2a555d7dd79671 to your computer and use it in GitHub Desktop.
const api = {
async getTotalFrogs() {
return {
data: {
result: [
{ name: 'bob the frog', tongueWidth: 50, weight: 8 },
{ name: 'joe the other frog', tongueWidth: 40, weight: 5 },
{ name: 'kelly the last frog', tongueWidth: 20, weight: 2 },
],
},
}
},
}
const getData = async ({ withTongues = false }) => {
try {
const response = await api.getTotalFrogs({ withTongues })
return response.data.result
} catch (err) {
throw err
}
}
const DataList = (props) => {
const [items, setItems] = useState([])
const [error, setError] = useState(null)
React.useEffect(() => {
getData({ withTongues: true })
.then(setItems)
.catch(setError)
}, [])
return (
<div>
{Array.isArray(items) && (
<Header size="tiny" inverted>
{items.map(({ name, tongueWidth, weight }) => (
<div style={{ margin: '25px 0' }}>
<div>Name: {name}</div>
<div>Width of their tongue: {tongueWidth}cm</div>
<div>Weight: {weight}lbs</div>
</div>
))}
</Header>
)}
{error && <Header>You received an error. Do you need a linter?</Header>}
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment