Created
August 12, 2019 16:24
-
-
Save djD-REK/e87fbbdf594f0abf34945359b25a8ec6 to your computer and use it in GitHub Desktop.
Used in the Medium article 2019 Guide: How to Use React Hooks to Create a Toggle Switch or Counter, from my Github true-false-toggle-using-react-hooks-demo
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 ReactDOM from "react-dom"; | |
import "./styles.css"; | |
function App() { | |
const [toggleState, setToggleState] = useState({ isTrue: false }); | |
const [counterState, setCounterState] = useState({ counter: 0 }); | |
const toggleTrueFalse = () => { | |
setToggleState({ isTrue: !toggleState.isTrue }); | |
}; | |
const getTrueFalseAsString = () => { | |
return toggleState.isTrue.toString(); | |
}; | |
const incrementCounter = () => { | |
setCounterState({ counter: counterState.counter + 1 }); | |
}; | |
return ( | |
<div className="App"> | |
<div className="container"> | |
<h1> | |
<span role="img" aria-label="Decorative Emojis"> | |
⭐️❤️☕🍍☄⛱⛷☀️☘️ | |
</span> | |
</h1> | |
<h2>Your toggle is {getTrueFalseAsString()} 🥳</h2> | |
<button onClick={toggleTrueFalse}> | |
<h3>Toggle me</h3> | |
</button> | |
<h2>You clicked below {counterState.counter} times 🥳</h2> | |
<button onClick={incrementCounter}> | |
<h3>Click me</h3> | |
</button> | |
<h1> | |
<span role="img" aria-label="Decorative Emojis"> | |
⭐️❤️☕🍍☄⛱⛷☀️☘️ | |
</span> | |
</h1> | |
</div> | |
</div> | |
); | |
} | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment