Forked from djD-REK/react-hooks-toggle-switch-counter-demo.js
Created
January 31, 2020 23:51
-
-
Save hemepositive/229d830da4ea02cd39324f8cf083cc5b to your computer and use it in GitHub Desktop.
Use React Hooks to Create a Toggle Switch or Counter
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 React, { useState } from "react"; | |
import ReactDOM from "react-dom"; | |
import "./styles.css"; | |
function App() { | |
const [isToggled, setToggled] = useState(false); | |
const [counterState, setCounterState] = useState(0); | |
const toggleTrueFalse = () => setToggled(!isToggled); | |
const incrementCounter = () => setCounterState(counterState + 1); | |
return ( | |
<div className="App"> | |
<div className="container"> | |
<h1> | |
<span role="img" aria-label="Decorative Emojis"> | |
⭐️❤️☕🍍☄⛱⛷☀️☘️ | |
</span> | |
</h1> | |
<h2>Your toggle is {isToggled.toString()} 🥳</h2> | |
<button onClick={toggleTrueFalse}> | |
<h3>Toggle me</h3> | |
</button> | |
<h2>You clicked below {counterState} 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