Skip to content

Commit f84a069

Browse files
committed
instructions
1 parent 8a9f98c commit f84a069

File tree

11 files changed

+92
-0
lines changed

11 files changed

+92
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
11
# 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!

exercises/01.use-state/01.problem.initial-state/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function Counter() {
1010
const [count, setCount] = useState(0)
1111
// 🦺 you'll get an error for this we'll fix that next
1212
const increment = () => setCount(count + 1)
13+
1314
return (
1415
<div className="counter">
1516
<button onClick={increment}>{count}</button>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
# Initial State
2+
3+
👨‍💼 Great work! Now at least we're not just throwing an error. But let's handle
4+
the state update...

exercises/01.use-state/01.solution.initial-state/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function Counter() {
1010
const [count, setCount] = useState(0)
1111
// @ts-expect-error we'll fix this soon
1212
const increment = () => setCount(count + 1)
13+
1314
return (
1415
<div className="counter">
1516
<button onClick={increment}>{count}</button>

exercises/01.use-state/02.problem.update-state/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function Counter() {
1212
const [count, setCount] = useState(0)
1313
// @ts-expect-error 💣 delete this comment
1414
const increment = () => setCount(count + 1)
15+
1516
return (
1617
<div className="counter">
1718
<button onClick={increment}>{count}</button>

exercises/01.use-state/02.solution.update-state/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function useState<State>(initialState: State) {
99
function Counter() {
1010
const [count, setCount] = useState(0)
1111
const increment = () => setCount(count + 1)
12+
1213
return (
1314
<div className="counter">
1415
<button onClick={increment}>{count}</button>

exercises/01.use-state/03.problem.re-render/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function useState<State>(initialState: State) {
1010
function Counter() {
1111
const [count, setCount] = useState(0)
1212
const increment = () => setCount(count + 1)
13+
1314
return (
1415
<div className="counter">
1516
<button onClick={increment}>{count}</button>

exercises/01.use-state/03.solution.re-render/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function useState<State>(initialState: State) {
1212
function Counter() {
1313
const [count, setCount] = useState(0)
1414
const increment = () => setCount(count + 1)
15+
1516
return (
1617
<div className="counter">
1718
<button onClick={increment}>{count}</button>

exercises/01.use-state/04.problem.preserve-state/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function useState<State>(initialState: State) {
2121
function Counter() {
2222
const [count, setCount] = useState(0)
2323
const increment = () => setCount(count + 1)
24+
2425
return (
2526
<div className="counter">
2627
<button onClick={increment}>{count}</button>

exercises/01.use-state/README.mdx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
11
# 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!

0 commit comments

Comments
 (0)