|
| 1 | +Let's quickly add some state to a simple little counter. For starters, this is basically the entire implementation. |
| 2 | + |
| 3 | +````tsx |
| 4 | +const Counter = () => { |
| 5 | + return ( |
| 6 | + <section className="flex flex-col items-center w-2/3 gap-8 p-8 bg-white border-4 shadow-lg border-primary-500"> |
| 7 | + <h1>Days Since the Last Accident</h1> |
| 8 | + <p className="text-6xl">0</p> |
| 9 | + <div className="flex gap-2"> |
| 10 | + <button>➖ Decrement</button> |
| 11 | + <button>🔁 Reset</button> |
| 12 | + <button>➕ Increment</button> |
| 13 | + </div> |
| 14 | + <div> |
| 15 | + <form onSubmit={(e) => e.preventDefault()}> |
| 16 | + <input type="number" value={0} /> |
| 17 | + <button type="submit">Update Counter</button> |
| 18 | + </form> |
| 19 | + </div> |
| 20 | + </section> |
| 21 | + ); |
| 22 | +}; |
| 23 | + |
| 24 | +export default Counter; |
| 25 | +```` |
| 26 | + |
| 27 | +This is nothing particularly special to see here. Adding some state can be done swiftly using the `useState` hook. |
| 28 | + |
| 29 | +````tsx |
| 30 | +const [count, setCount] = useState(0); |
| 31 | +```` |
| 32 | + |
| 33 | +And then—yea—we'll add some state to our counter, just like every other modern React tutorial you've ever seen. |
| 34 | + |
| 35 | +````diff |
| 36 | +@@ -1,12 +1,16 @@ |
| 37 | ++import { useState } from 'react'; |
| 38 | ++ |
| 39 | + const Counter = () => { |
| 40 | ++ const [count, setCount] = useState(0); |
| 41 | ++ |
| 42 | + return ( |
| 43 | + <section className="flex flex-col items-center w-2/3 gap-8 p-8 bg-white border-4 shadow-lg border-primary-500"> |
| 44 | + <h1>Days Since the Last Accident</h1> |
| 45 | +- <p className="text-6xl">0</p> |
| 46 | ++ <p className="text-6xl">{count}</p> |
| 47 | + <div className="flex gap-2"> |
| 48 | +- <button>➖ Decrement</button> |
| 49 | +- <button>🔁 Reset</button> |
| 50 | +- <button>➕ Increment</button> |
| 51 | ++ <button onClick={() => setCount(count - 1)}>➖ Decrement</button> |
| 52 | ++ <button onClick={() => setCount(0)}>🔁 Reset</button> |
| 53 | ++ <button onClick={() => setCount(count + 1)}>➕ Increment</button> |
| 54 | + </div> |
| 55 | + <div> |
| 56 | + <form onSubmit={(e) => e.preventDefault()}> |
| 57 | +```` |
| 58 | + |
| 59 | +We'll worry about the form in a hot minute. |
| 60 | + |
| 61 | +## Taking a look at what TypeScript has figured out for us |
| 62 | + |
| 63 | +So, this is where it gets kind of cool. This file doesn't look like TypeScript, but it *is*. |
| 64 | + |
| 65 | +If you hover over `count`, you'll see that TypeScript was about to deduce that it's a number because you set its initial value to a number. |
| 66 | + |
| 67 | +````ts |
| 68 | +const count: number; |
| 69 | +```` |
| 70 | + |
| 71 | +You can also see that it figured out that `setCount` should *only* take a number, which means that `count` will *always* be a number. |
| 72 | + |
| 73 | +````ts |
| 74 | +const setCount: React.Dispatch<React.SetStateAction<number>>; |
| 75 | +```` |
| 76 | + |
| 77 | +Don't worry about all of that ceremony. `useState` is an abstraction over `useReducer`, which works by dispatching actions. |
| 78 | + |
| 79 | +If we try to break the rules, you'll see that TypeScript keeps us honest. |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +This is *particulary* helpful when dealing with one of my least favorite parts of the browser: the fact that number inputs store their values as strings. 🙄 |
| 84 | + |
| 85 | +Let's take this head on and try to [get that little form for manually setting the count working](useState,%20an%20exercise.md). |
0 commit comments