Skip to content

Commit 7376aa5

Browse files
committed
finish instructions
1 parent 2e1a649 commit 7376aa5

File tree

22 files changed

+277
-6
lines changed

22 files changed

+277
-6
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Initial State
22

33
👨‍💼 Great work! Now at least we're not just throwing an error. But let's handle
4-
the state update...
4+
the state update next.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
# Update State
2+
3+
👨‍💼 Alright, right now when you click the button, nothing happens. Let's get it
4+
to update the state.
5+
6+
Update the `setState` function to assign the given state to the new state.
7+
8+
<callout-warning>
9+
When you do this, it'll not actually update the number in the button either.
10+
We'll get to that soon!
11+
</callout-warning>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
# Update State
2+
3+
👨‍💼 So we're updating the state value, but it's not actually updating the number
4+
in the button? What gives?!
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
11
# Re-render
2+
3+
👨‍💼 Ok, so we're initializing our state properly and we're updating the state
4+
properly as well. The problem is we're not updating the UI when the state gets
5+
updated. Remember, we're not React. We need to tell React when the state has
6+
changed so it will render our component again.
7+
8+
Because we're not React, the way we will do this is by simply calling `render`
9+
our our root again. Remember what the bottom of our file looks like?
10+
11+
```tsx lines=4
12+
const rootEl = document.createElement('div')
13+
document.body.append(rootEl)
14+
const appRoot = createRoot(rootEl)
15+
appRoot.render(<Counter />)
16+
```
17+
18+
That last line there is where we render the component. So we just need to call
19+
that any time we want the component updated!
20+
21+
So in this exercise, wrap that bit in a function and call it once for the
22+
initial render and once in the `setState` function.
23+
24+
Feel free to toss in a `console.log` in the component to make sure it's
25+
re-rendering.
26+
27+
<callout-warning>
28+
When you're finished with this, the UI will **still** not work like you
29+
expect. I promise we'll get to that very soon!
30+
</callout-warning>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# Re-render
2+
3+
👨‍💼 Great work! Now we're not only updating the state, but we're also triggering
4+
a re-render so the UI can be updated. Unfortunately that seems to not be working
5+
either? Let's figure out why.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
# Preserve State
2+
3+
👨‍💼 Alright, so there are actually two problems here. First, when the user clicks
4+
on the button, we update the `state` variable inside the `useState` closure, but
5+
that variable is not accessible by our component. Our component has its own
6+
variable called `count` which is not being updated
7+
8+
<callout-info>
9+
Just because two variables point to the same object in memory (or in our case,
10+
the same number) doesn't mean they stay in sync when one is reassigned. That's
11+
just how JavaScript works.
12+
</callout-info>
13+
14+
The second problem we have is when our component is called, it calls `useState`
15+
and that creates a brand new `state` variable that's assigned to the
16+
`initialState` variable again.
17+
18+
So we need a way to preserve the state between renders. We can do that by
19+
pulling the `state` and `setState` variables outside the `useState` hook and
20+
simply assigning them on the initial render of the component.
21+
22+
Give that a try!
23+
24+
<callout-success>
25+
The button will finally work on this one, I promise!
26+
</callout-success>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
# Preserve State
2+
3+
👨‍💼 Great work! Our UI is working properly! By preserving our state we're able to
4+
make changes to it and render again whenever that value changes.

exercises/01.use-state/FINISHED.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
# useState
2+
3+
👨‍💼 This is a great time for you to take a break and reflect on your learnings so
4+
far. Take a moment and then when you're ready, we'll see you in the next
5+
exercise.
6+
7+
There's still plenty to do!
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
11
# Render Phase
2+
3+
🧝‍♂️ Hi! I made a change to the code a bit. Now we're rendering two buttons, the
4+
count button is still there, but now we're also a button for disabling the count
5+
button. I needed to add another `useState` for that, but it's not working. You
6+
can <PrevDiffLink>check my work</PrevDiffLink> if you'd like. Can you get it
7+
working? Thanks!
8+
9+
👨‍💼 Thanks for adding those buttons Kellie!
10+
11+
Ok, so what we need you to do is fix the problem. If you add a
12+
`console.log({ count, enabled })` to the component, you'll get
13+
`{ count: 0, enabled: 0 }`. This is because the first time the `useState` is
14+
called initializes the state and the second time it's called, it just references
15+
the first one.
16+
17+
So to start off fixing this issue, you're going to need to formalize how we
18+
determine whether state gets initialized or referenced.
19+
20+
Really, `useState` can be called in two scenarios:
21+
22+
- Initialization
23+
- Updates
24+
25+
So we're going to keep track of how this is called with a `phase` variable. The
26+
emoji will guide you in the right direction. Good luck!
27+
28+
<callout-warning>
29+
Note it's not going to quite work when you're finished with this step, but
30+
it'll work soon!
31+
</callout-warning>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
# Render Phase
2+
3+
👨‍💼 Great! It's not quite working yet, now if we add
4+
`console.log({ count, enabled })` to the component, we'll get
5+
`{ count: 0, enabled: true }` like you'd expect, but when you click the counter
6+
button we get `{ count: 1, enabled: 1 }` 😅. And if you click the disable button
7+
you get `{ count: false, enabled: false }`. What the heck is going on!? Let's
8+
find out.

0 commit comments

Comments
 (0)