Workshop 2: Catbook in React
Akshaj Kadaveru
1
weblab.is/questions
weblab.is/q
How to write any website
2
<App />
1 line of code!
React Apps are "components of components"
The App component (<App />)
React Apps are "components of components"
<Navbar />
<Feed />
<Sidebar />
4
React Apps are "components of components"
<Post />
<Post />
<Post /> ...
<Storysection />
<Suggestions />
5
React Apps are "components of components"
<Postbody />
<Comment />
<Comment /> ...
6
Props ('Inputs')
Inputs passed from a parent to a child component
7
<Post name="Akshaj" text="Welcome to web lab!" />
These are all props! (the inputs)
here, props = {name: "Akshaj", text: "Welcome to web lab!"}
State
Updatable pieces of information maintained by a component.
const [status, setStatus] = useState("busy");
const [isOnline, setIsOnline] = useState(false);
8
Big Picture (of a random app)
9
Another component
state: currentName
A fifth component
App
A fourth component
A component
state: numberOfLikes
A sixth component
e.g. state: currentYear, typeOfCar
A seventh component
props (e.g. name='joe'
numCats=4)
props
(e.g. phoneType="iPhone")
Big Picture
10
Feed
Post
App
Sidebar
Navbar
const App = () => {
return (
<div>
<Navbar />
<div>
<Feed />
<Sidebar />
</div>
</div>
)
}
App
// App.js
Big Picture
11
Feed
Post
App
Sidebar
Navbar
const Sidebar = () => {
return (
<div>
<StorySection />
<Suggestions /> </div>
)
}
Sidebar
// Sidebar.js
Suggestions
StorySection
Big Picture
12
Feed
Post
App
Sidebar
Navbar
const Navbar = () => {
return (
<div>
<img src="instaIcon.png" />
<SearchBar />
<NavButtons />
</div>
)
}
Navbar
// Navbar.js
NavButtons
SearchBar
Big Picture
13
Feed
Post
App
Sidebar
Navbar
const Feed = () => {
return (
<div>
<Post />
<Post />
<Post />
<Post />
</div>
)
}
Feed
// Feed.js
Big Picture
14
Feed
Post
App
Sidebar
Navbar
const Post = (props) => {
// declare some state variables here
// do whatever javascript stuff/calculations you want to do here
return (
*a bunch of HTML code*
)
}
// Post.js
Big Picture
15
Feed
Post
App
Sidebar
Navbar
const Post = (props) => {
return (
<div>
<img src={props.content} />
<button>
<img src="like.png" />
{250}
</button>
</div>
)
}
// Post.js
250
Big Picture
16
Feed
Post
App
Sidebar
Navbar
const Post = (props) => {
const [numberOfLikes, setNumberOfLikes] = useState(0);
return (
<div>
<img src={props.content} />
<button>
<img src="like.png" />
{numberOfLikes}
</button>
</div>
)
}
// Post.js
250
Big Picture
17
Feed
Post
App
Sidebar
Navbar
const Post = (props) => {
const [numberOfLikes, setNumberOfLikes] = useState(0);
const addLike = () => { setNumberOfLikes(numberOfLikes + 1); }
return (
<div>
<img src={props.content} />
<button onClick={addLike}>
<img src="like.png" />
{numberOfLikes}
</button>
</div>
)
}
// Post.js
250
Recap
18
Check out our recap guide at weblab.to/react-guide-1
19
20
Let's make sure everyone has the right version of node
Type
node -v
you should get '16.3.1' or '16.x.y'
21
1. Open the catbook-react folder in VS Code
2. Open the terminal window in VS Code, make sure you are in the 'catbook-react' folder
3. Run the following commands:
git fetch
git reset --hard�git checkout w2-starter
npm install
First: the component tree for Catbook!
22
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
App
NavBar
CatHappiness
Profile
First: the component tree for Catbook!
23
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
App
NavBar
CatHappiness
Profile
First: the component tree for Catbook!
24
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
App
NavBar
CatHappiness
Profile
npm run hotloader
Navigate to localhost:5000 and see the page update with your live changes!
25
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Let's look at the starter code
26
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Writing Components
27
Exercise 1: Implementing React Navbar
<div className="style-name">
28
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Exercise 1a: React Navbar CSS
29
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Exercise 1b: React Navbar JS
// NavBar.js
30
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Let's get on the same page
git reset --hard�git checkout w2-step1
31
*If it doesn't let you checkout and says 'Please commit your changes or stash them', then 'git stash' should do the trick and you should be able to checkout
Save or close out of all of your 'unsaved' files:
Adding CatHappiness
32
Which component should we store 'catHappiness' in?
33
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
App
NavBar
CatHappiness
Profile
Why Profile? We want to update 'catHappiness' when the cat is clicked
34
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
App
NavBar
CatHappiness
Profile
Clicking on the cat needs to update catHappiness, and the cat is in Profile!
35
Profile Component
State: catHappiness (int)
CatHappiness Component
Prop: catHappiness (int)
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
App
NavBar
How do we add state to a component?
36
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
How do we add state to a component?
... we use useState!!
37
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Exercise 2a: Add the 'catHappiness' state variable to Profile
In profile.js.
Also don't forget to import useState (import React, { useState } from "react")
Use the React Guide (weblab.to/react-guide-1) if you're stuck!
38
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Exercise 2a: Add the 'catHappiness' state to Profile
// Profile.js (also don't forget to import useState)
39
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Exercise 2b: Import the CatHappiness Component
// Profile.js
40
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Exercise 3a: Add the CatHappiness component
41
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
42
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Are we done?
43
Exercise 3b: Display the incoming CatHappiness Prop
44
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
// CatHappiness.js
Exercise 3b: Use the incoming the CatHappiness Prop
45
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
// CatHappiness.js
Let's get on the same page
git reset --hard�git checkout w2-step2
46
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
*If it doesn't let you checkout and says 'Please commit your changes or stash them', then 'git stash' should do the trick and you should be able to checkout
Save or close out of all of your 'unsaved' files:
Exercise 4: Update CatHappiness State
Now we need to change the CatHappiness when we click!
47
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Exercise 4: Update CatHappiness State
48
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
HINT: All divs have an 'onClick' prop that takes a function. Whenever a div is clicked, it runs its onClick function.
49
Works, just not the most readable. Also unnecessary since we aren't doing anything else inside this function.
Works and super clean code!! Recommended implementation!
Doesn't work since it will execute the function when the div element is created, not when it's clicked on.
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Also pretty good
Let's get on the same page
git reset --hard�git checkout w2-complete
Navigate to localhost:5000 and change the cat happiness by clicking the profile picture!
50
starter
step1
step2
complete
workshop 3 progress
starter
step1
step2
workshop 2 progress
complete
Save or close out of all of your 'unsaved' files:
Recap: Writing Components
51
's _ _ _ e' variables
j _ _ _ _ _ _ _ _ t environment
Recap: Writing Components
52
const [something, setSomething] = useState(initialValue)
53