Created
November 6, 2022 01:01
-
-
Save koraytugay/d4bede880c63e6a80214cef8eead531f to your computer and use it in GitHub Desktop.
React useState Example
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 {useState} from 'react'; | |
const App = () => { | |
const [counter, setCounter] = useState(0); | |
const incrementHandler = () => setCounter(counter + 1); | |
const decrementHandler = () => setCounter(counter - 1); | |
return ( | |
<> | |
<Button onClick={decrementHandler} text='Decrement'/> | |
<Button onClick={incrementHandler} text='Increment'/> | |
<Display counter={counter}/> | |
</> | |
); | |
}; | |
const Display = ({counter}) => <div>{counter}</div>; | |
const Button = ({onClick, text}) => <button onClick={onClick}>{text}</button>; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment