Skip to content

Commit fbaa2b1

Browse files
committed
good progress on exercise 1
1 parent 4d3fce2 commit fbaa2b1

File tree

10 files changed

+65
-11
lines changed

10 files changed

+65
-11
lines changed

β€ŽREADME.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@
3333

3434
## Prerequisites
3535

36-
- Watch my talk
37-
[Why React Hooks](https://www.youtube.com/watch?v=zWsZcBiwgVE&list=PLV5CVI1eNcJgNqzNwcs4UKrlJdhfDjshf)
38-
(35 minutes)
36+
- Experience with
37+
[React fundamentals](https://github.com/epicweb-dev/react-fundamentals)
3938

4039
## Pre-workshop Resources
4140

4241
Here are some resources you can read before taking the workshop to get you up to
4342
speed on some of the tools and concepts we'll be covering:
4443

45-
- TODO: add resources
44+
- [Why React Hooks](https://www.youtube.com/watch?v=zWsZcBiwgVE&list=PLV5CVI1eNcJgNqzNwcs4UKrlJdhfDjshf)
45+
(35 minutes)
46+
- [Getting Closure on Hooks](https://www.swyx.io/getting-closure-on-hooks/)
47+
- [React Hooks Documentation](https://react.dev/reference/react/hooks)
4648

4749
## System Requirements
4850

Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
# useState
2+
3+
πŸ‘¨β€πŸ’Ό We've got you working on a blog search page. Allow me to introduce you to
4+
πŸ§β€β™‚οΈ Kellie the coworker who put this together.
5+
6+
πŸ§β€β™‚οΈ Hello! I'm Kellie the co-worker and I do things for you sometimes. I've
7+
started up this app that you're going to be working with. There's a search
8+
input, a couple checkboxes, a submit button, and a list of blog posts. You've
9+
been asked to make it so when the user types into the search field it filters
10+
the list of blog posts. And when the checkboxes are checked, we want to update
11+
the filter for the user to include those values. You might find it helpful to
12+
review the code before you get started.
13+
14+
πŸ‘¨β€πŸ’Ό Thanks Kellie! Ok, so we're going to take this step-by-step. For this first
15+
step, we just want you to create a state variable for the user's query and pass
16+
that along to the `<MatchingPosts />`.

β€Žexercises/01.managing-ui-state/01.problem.use-state/index.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { generateGradient, getMatchingPosts } from '#shared/blog-posts'
33

44
function App() {
55
// 🐨 call useState here and initialize the query with an empty string
6-
// πŸ’° we don't need the query yet, but you can console.log it if you want
76

87
return (
98
<div className="app">
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
# useState
2+
3+
πŸ‘¨β€πŸ’Ό Super! You've successfully filtered the blog posts based on what the user has
4+
typed. This is where we start getting into why React is so great. It makes doing
5+
stateful dynamic things like this super easy compared to the alternative of
6+
working with the DOM directly. Let's keep going.

β€Žexercises/01.managing-ui-state/01.solution.use-state/index.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { generateGradient, getMatchingPosts } from '#shared/blog-posts'
44

55
function App() {
66
const [query, setQuery] = useState('')
7-
console.log(query)
87

98
return (
109
<div className="app">
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
# Controlling Inputs
2+
3+
πŸ‘¨β€πŸ’Ό When the user clicks on one of the checkboxes, we want to auto-fill that
4+
value into the search input for them. This means we need to "control" the input
5+
value so we can set it programmatically. We can do this by using the `value`
6+
prop on the input.
7+
8+
πŸ“œ You can read up more on this in the React docs:
9+
[Controlling an input with a state variable](https://react.dev/reference/react-dom/components/input#controlling-an-input-with-a-state-variable)
10+
11+
Once we add the query as the value for the input, we'll also want to have a
12+
function responsible for updating the query when the user checks or unchecks the
13+
checkboxes and call that in the `onClick` handler of the checkboxes.
14+
15+
Good luck!

β€Žexercises/01.managing-ui-state/02.problem.control/index.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { generateGradient, getMatchingPosts } from '#shared/blog-posts'
44

55
function App() {
66
const [query, setQuery] = useState('')
7-
console.log(query)
87

98
// 🐨 make a function called handleCheck that accepts a "tag" string and a "checked" boolean
109
// 🐨 By calling setQuery, add the tag to the query if checked and remove it if not

β€Žexercises/01.managing-ui-state/README.mdx

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ you use special functions called "hooks" to do this. Common built-in hooks
55
include:
66

77
- `useState`
8-
- `useEffect`
9-
- `useContext`
108
- `useRef`
9+
- `use` (currently in canary)
1110
- `useReducer`
11+
- `useEffect`
1212

1313
Each of these is a special function that you can call inside your custom React
1414
component function to store data (like state) or perform actions (or
1515
side-effects). There are a few more built-in hooks that have special use cases,
1616
but the ones above are what you'll be using most of the time.
1717

1818
Each of the hooks has a unique API. Some return a value (like `useRef` and
19-
`useContext`), others return a pair of values (like `useState` and
19+
`use`), others return a pair of values (like `useState` and
2020
`useReducer`), and others return nothing at all (like `useEffect`).
2121

2222
Here's an example of a component that uses the `useState` hook and an onClick
2323
event handler to update that state:
2424

25-
```jsx
25+
```tsx
2626
function Counter() {
2727
const [count, setCount] = useState(0)
2828
const increment = () => setCount(count + 1)
@@ -51,6 +51,11 @@ called this time, the value we get back is the value that we called `setCount`
5151
with. And it continues like that until `Counter` is unmounted (removed from the
5252
application), or the user closes the application.
5353

54+
Note that after the initial render, the argument passed to `useState` is
55+
ignored. The only time it's used is when the component is first created. The
56+
only way to change the state value of the component while it's around is to
57+
call the updater function returned by `useState`.
58+
5459
πŸ“œ To get a deeper dive on how React keeps track of hooks, read/watch this great
5560
post/talk by [Shawn Wang](https://twitter.com/swyx):
5661
[Getting Closure on Hooks](https://www.swyx.io/getting-closure-on-hooks/)

β€Žexercises/README.mdx

+15
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
# React Hooks 🎣
2+
3+
πŸ‘¨β€πŸ’Ό Hello! I'm Peter the Product manager and I'm here to help guide you through
4+
this workshop. I'll be there to tell you what our users want and to help you
5+
understand the requirements of the tasks you'll be working on.
6+
7+
React hooks are an critical part of React development. You can think of them as
8+
atoms to the React component molecule. Everything outside of basic rendering
9+
can be done with hooks.
10+
11+
In this workshop, we'll learn how to use hooks to manage state, synchronize
12+
side-effects, generate unique ids, and more. There's a lot to hooks and we'll
13+
get into more advanced use cases in a future workshop. In this one you'll get
14+
a solid understanding of the basic hooks and how to use them.
15+
16+
So let's get started!

β€Žkcdshop/.diffignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tsconfig.json

0 commit comments

Comments
Β (0)