Created
January 24, 2019 15:21
-
-
Save treyhuffine/c15775cbed57c33d4121c129d8517dfc to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
import './App.css'; | |
const INCREMENTS = [1, 2, 5, 10]; | |
const mockApi = () => { | |
return new Promise(resolve => { | |
setTimeout(() => { | |
const randomInt = Math.ceil(Math.random() * 10); | |
resolve(randomInt); | |
}, 1000); | |
}); | |
}; | |
const App = () => { | |
const [count, setCount] = useState(0); | |
const [hasFetched, setFetch] = useState(false); | |
useEffect(async () => { | |
if (!hasFetched) { | |
const apiResponse = await mockApi(); | |
setFetch(true); | |
setCount(count + apiResponse); | |
} | |
}); | |
useEffect(() => { | |
document.title = 'Count: ' + count; | |
}); | |
return ( | |
<div className="App"> | |
<div>Count: {count}</div> | |
{hasFetched ? ( | |
<div> | |
{INCREMENTS.map(value => { | |
return <button onClick={() => setCount(count + value)}>+{value}</button>; | |
})} | |
</div> | |
) : ( | |
<div>Loading...</div> | |
)} | |
</div> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment