Created
January 28, 2021 16:23
-
-
Save humphd/e353ab107e561c496bf9eec78fa8cac4 to your computer and use it in GitHub Desktop.
WEB422 Week 4 - React Events
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 { useState } from 'react'; | |
import ClickCounter from './ClickCounter'; | |
import ClickHeading from './ClickHeading'; | |
function App() { | |
// State: held by the parent | |
const [numClicks, setNumClicks] = useState(0); | |
const onClickHandler = (e) => setNumClicks(numClicks + 1); | |
return ( | |
<div> | |
<ClickHeading clicks={numClicks} /> | |
<ClickCounter clicks={numClicks} onClick={onClickHandler} /> | |
</div> | |
); | |
} | |
export default App; |
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
function ClickCounter({ clicks, onClick }) { | |
return ( | |
<button | |
className="click-button" | |
onClick={onClick} | |
>Clicks {clicks}</button> | |
); | |
} | |
export default ClickCounter; |
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
export default function ClickHeading({ clicks }) { | |
if(clicks === 0) { | |
return null; | |
} | |
const times = clicks > 1 ? 'times' : 'time'; | |
return ( | |
<h1> | |
You've clicked { clicks } { times } | |
</h1> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment