Created
July 12, 2022 01:14
-
-
Save drteresavasquez/6ae462e97d0bb39e40182fe3c919058b to your computer and use it in GitHub Desktop.
Counter App Example
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 } from 'react'; | |
import PropTypes from 'prop-types'; | |
export default function Counter({ title }) { | |
const [value, setValue] = useState(0); | |
const handleClick = () => { | |
setValue((prevState) => prevState + 1); | |
}; | |
return ( | |
<div> | |
{value === 5 ? ( | |
'YOU DA WIENER' | |
) : ( | |
<> | |
<h1>{title}</h1> | |
<h2>{value}</h2> | |
<button type="button" className={value <= 0 ? 'btn btn-danger' : 'btn btn-success'} onClick={handleClick}> | |
Increment | |
</button> | |
{value <= 0 ? ( | |
'' | |
) : ( | |
<button type="button" onClick={() => setValue((prevState) => prevState - 1)}> | |
Decrement | |
</button> | |
)} | |
<button type="button" onClick={() => setValue(0)}> | |
Reset | |
</button> | |
</> | |
)} | |
</div> | |
); | |
} | |
Counter.propTypes = { | |
title: PropTypes.string, | |
}; | |
Counter.defaultProps = { | |
title: 'Counter Default', | |
}; |
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 Counter from '../components/Counter'; | |
function Home() { | |
const counters = [{ title: 'Counter 1' }, { title: 'Counter 2' }, { title: 'Counter 3' }]; | |
return ( | |
<> | |
{counters.map((counter) => ( | |
<Counter key={counter.title} title={counter.title} /> | |
))} | |
</> | |
); | |
} | |
export default Home; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment