Created
August 8, 2025 17:43
-
-
Save tobySolutions/0347e72a297f1b59b6a8c340d23a3bc2 to your computer and use it in GitHub Desktop.
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 { useEffect, useState } from 'react' | |
| import './App.css' | |
| function App() { | |
| const [count, setCount] = useState<number>(0) | |
| const [myState, setMyState] = useState<{ [key: string]: any }>({"toby": 10}) | |
| const [toggleSwitch, setToggleSwitch] = useState<boolean>(true) | |
| const [color, setColor] = useState<string>("blue"); | |
| // useState | |
| // useEffect | |
| // useRef | |
| useEffect(() => { | |
| if (toggleSwitch === true) { | |
| setColor("red"); | |
| } | |
| }, []) | |
| return ( | |
| <main | |
| style={{backgroundColor: `${toggleSwitch ? color : "white"}`}}> | |
| <h1>Vite + React</h1> | |
| <div className="card"> | |
| <button onClick={() => setCount((count) => count + 1)}> | |
| count is {count} | |
| </button> | |
| <button onClick={() => setMyState({ "toby": myState["toby"] + 1 })}> | |
| Toby is {myState["toby"]} | |
| </button> | |
| <button onClick={() => setToggleSwitch(!toggleSwitch)}> | |
| Toggle Switch is {toggleSwitch ? "ON" : "OFF"} | |
| </button> | |
| <p> | |
| Edit <code>src/App.tsx</code> and save to test HMR | |
| </p> | |
| </div> | |
| </main> | |
| ) | |
| } | |
| export default App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment