File tree Expand file tree Collapse file tree 11 files changed +92
-0
lines changed
01.solution.initial-state
04.problem.preserve-state Expand file tree Collapse file tree 11 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 1
1
# Initial State
2
+
3
+ 👨💼 Hello! So here's where we're starting out:
4
+
5
+ ``` tsx
6
+ import { createRoot } from ' react-dom/client'
7
+
8
+ function Counter() {
9
+ const [count, setCount] = useState (0 )
10
+ const increment = () => setCount (count + 1 )
11
+
12
+ return (
13
+ <div className = " counter" >
14
+ <button onClick = { increment } >{ count } </button >
15
+ </div >
16
+ )
17
+ }
18
+
19
+ const rootEl = document .createElement (' div' )
20
+ document .body .append (rootEl )
21
+ const appRoot = createRoot (rootEl )
22
+ appRoot .render (<Counter />)
23
+ ```
24
+
25
+ Pretty simple, except the ` useState ` function isn't defined and we're not
26
+ allowed to simply import it from React because we're not allowed to directly use
27
+ any React hooks in this workshop!
28
+
29
+ And because it's not defined, our ` Counter ` component won't be able to render
30
+ anything (because an error will be thrown).
31
+
32
+ So let's just get it to the point where the ` Counter ` component renders
33
+ initially without errors. When you're done, it should render a button with a ` 0 `
34
+ in it.
35
+
36
+ The emoji should lead the way!
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ function Counter() {
10
10
const [ count , setCount ] = useState ( 0 )
11
11
// 🦺 you'll get an error for this we'll fix that next
12
12
const increment = ( ) => setCount ( count + 1 )
13
+
13
14
return (
14
15
< div className = "counter" >
15
16
< button onClick = { increment } > { count } </ button >
Original file line number Diff line number Diff line change 1
1
# Initial State
2
+
3
+ 👨💼 Great work! Now at least we're not just throwing an error. But let's handle
4
+ the state update...
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ function Counter() {
10
10
const [ count , setCount ] = useState ( 0 )
11
11
// @ts -expect-error we'll fix this soon
12
12
const increment = ( ) => setCount ( count + 1 )
13
+
13
14
return (
14
15
< div className = "counter" >
15
16
< button onClick = { increment } > { count } </ button >
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ function Counter() {
12
12
const [ count , setCount ] = useState ( 0 )
13
13
// @ts -expect-error 💣 delete this comment
14
14
const increment = ( ) => setCount ( count + 1 )
15
+
15
16
return (
16
17
< div className = "counter" >
17
18
< button onClick = { increment } > { count } </ button >
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ function useState<State>(initialState: State) {
9
9
function Counter ( ) {
10
10
const [ count , setCount ] = useState ( 0 )
11
11
const increment = ( ) => setCount ( count + 1 )
12
+
12
13
return (
13
14
< div className = "counter" >
14
15
< button onClick = { increment } > { count } </ button >
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ function useState<State>(initialState: State) {
10
10
function Counter ( ) {
11
11
const [ count , setCount ] = useState ( 0 )
12
12
const increment = ( ) => setCount ( count + 1 )
13
+
13
14
return (
14
15
< div className = "counter" >
15
16
< button onClick = { increment } > { count } </ button >
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ function useState<State>(initialState: State) {
12
12
function Counter ( ) {
13
13
const [ count , setCount ] = useState ( 0 )
14
14
const increment = ( ) => setCount ( count + 1 )
15
+
15
16
return (
16
17
< div className = "counter" >
17
18
< button onClick = { increment } > { count } </ button >
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ function useState<State>(initialState: State) {
21
21
function Counter ( ) {
22
22
const [ count , setCount ] = useState ( 0 )
23
23
const increment = ( ) => setCount ( count + 1 )
24
+
24
25
return (
25
26
< div className = "counter" >
26
27
< button onClick = { increment } > { count } </ button >
Original file line number Diff line number Diff line change 1
1
# useState
2
+
3
+ The ` useState ` hook is one of the most common hooks and is a good place for us
4
+ to start because it triggers re-rendering of the component and we can observe
5
+ its effects in the UI.
6
+
7
+ The goal of this exercise is not to build something super rigourous, but instead
8
+ to hack away until it works.
9
+
10
+ Here's the basic API that we need to implement in this exercise:
11
+
12
+ ``` tsx
13
+ const [state, setState] = useState (initialState )
14
+
15
+ function handleEvent() {
16
+ setState (newState )
17
+ }
18
+ ```
19
+
20
+ The ` useState ` hook accepts an initial value, and returns an array with two
21
+ values:
22
+
23
+ - ` state ` : the current value of the state
24
+ - ` setState ` : a function that updates the state
25
+
26
+ We will use the ` setState ` function to update the state of the component. But
27
+ just updating the state is not enough. We need to trigger a re-render of the
28
+ component as well.
29
+
30
+ Because we're not React, we need to hack away at things a little bit. For
31
+ example, the built-in ` useState ` hook is _ the_ way to trigger re-renders, but
32
+ since we'll be implementing ` useState ` ourselves, we'll have to be creative on
33
+ how we trigger re-renders.
34
+
35
+ Another tricky thing will be ensuring we can keep track of the state external to
36
+ our component so when the component gets rendered again we can access the latest
37
+ version of the state.
38
+
39
+ Let's get into it!
You can’t perform that action at this time.
0 commit comments