Powering Up With ReactJS
Powering Up With ReactJS
First Component
Level 1 Section 1
First Component
Writing Your First React Component
What Is React?
d
o
o
g
s
a
w
t
i
d
r
hea
(Model-View-Controller)
React is a JavaScript library for building user interfaces (UIs). Some people use it as the V in MVC.
Why React?
React was built to solve one problem: building large
applications with data that changes over time.
Conceived at Facebook
Prerequisites
JavaScript Basics
Declaring variables
Creating and invoking functions
ES2015
Class syntax
Arrow functions
Spread operator
Component
Component
<h1>Hello</h1>
Hello
Button
Type your name
Component-based Architecture
In React, we solve problems by creating components. If a component gets too complex, we break
it into smaller, simpler components.
StoryBox
StoryForm
adds new stories to the feed
Story
information about each story in our feed
Story component
is reused
Story
(root component)
Output #1
Calling render()
generates this
<div>
<p>Good Morning</p>
<p>10:45AM</p>
</div>
A React component
Output #2
Calling render()
generates this
<div>
<p>Good Morning</p>
<p>10:55AM</p>
</div>
(Step 1)
Virtual DOM
Component render
<div>
<p>Good Morning</p>
<p>10:45AM</p>
</div>
In-memory representation of
what will become real elements
(Step 2)
HTML
<div>
<p>Good Morning</p>
<p>10:45AM</p>
</div>
Virtual DOM
Component rendering
for the rst time
<div>
<p>Good Morning</p>
<p>10:45AM</p>
</div>
Virtual DOM
Component rendering
for the second time
<div>
<p>Good Morning</p>
<p>10:55AM</p>
</div>
HTML
<div>
<p>Good Morning</p>
<p>10:45AM</p>
</div>
Other elements
remain untouched HTML
<div>
<p>Good Morning</p>
<p>10:55AM</p>
</div>
Story Box
<div>Story Box</div>
No quotes needed
don't freak out.
Now we need to tell our application where to put the result into our web page.
components.js
StoryBox
Renderer
Invoke StoryBox
again, we don't need quotes
components.js
StoryBox
Renderer
Application Structure
components.js
...
ReactDOM.render(
<StoryBox />, document.getElementById('story-app')
);
index.html
<!DOCTYPE html>
Target
container
<html>
<body>
<div id="story-app"></div>
</body>
</html>
Thats all there is to creating a component. Now we just need to add libraries.
Application Structure
index.html
Project Folder
index.html
components.js
vendors
react.js
react-dom.js
babel.js
<!DOCTYPE html>
<html>
<body>
<div id="story-app"></div>
Holds all our React
<script src="vendors/react.js"></script>
components
<script src="vendors/react-dom.js"></script>
<script src="vendors/babel.js"></script>
<script type="text/babel"
src="components.js"></script>
</body>
</html>
React libraries
Allows using latest features of
JavaScript (class syntax, fat arrow, etc.)
Story Box
<script
<script
<script
StoryBox
Story Box
Virtual DOM
Level 1 Section 2
First Component
Understanding JSX
ReactDOM.render(
<StoryBox />, document.getElementById('story-app')
);
React.createElement("div", null,
React.createElement("h3", null, "Stories App"),
React.createElement("p", {className:"lead"}, "Sample paragraph")
);
JSX
JavaScript
React.createElement("div", null,
React.createElement("h3", null, "Stories App"),
React.createElement("p", {className:"lead"}, "Sample paragraph")
);
Generated HTML
Stories App
Current time: 16:56:26 GMT-0400 (EDT)
return (
<div>
<h3>Stories</h3>
<p className="lead">
Current time: {now.toTimeString()}
</p>
</div>
Code written within curly braces gets
);
}'
}"
Stories App
Current time: 16:56:26 GMT-0400 (EDT)
return (
<div>
...
<ul>
{topicsList.map( topic => <li>{topic}</li> )}
</ul>
</div>
);
}'
}"
HTML
JavaScript
React
<li>HTML</li>
<li>JavaScript</li>
<li>React</li>
Level 2
Level 2 Section 1
CommentBox
Comment
Comment
1. New class
HTML
<div class="comment-box">
<h3>Comments</h3>
<h4 class="comment-count">2 comments</h4>
<div class="comment-list">
<div class="comment">
<p class="comment-header">Anne Droid</p>
<p class="comment-body">
I wanna know what love is...
</p>
<div class="comment-footer">
<a href="#" class="comment-footer-delete">
Delete comment
Animation: magic move from here
</a>
</div>
</div>
CommentBox component
Comment component
<Comment />
Passing Props
<Comment
author="Morgan McCircuit"
body="Great picture!" />
Receiving Props
class Comment extends React.Component {
render() {
return(
...
<p className="comment-header">
{this.props.author}
</p>
<p className="comment-body">
{this.props.body}
</p>
...
Reads
arguments
passed
to
a
component
);
}
}
Level 2 Section 2
JavaScript
const commentList = [
{ id: 1, author: 'Morgan McCircuit', body: 'Great picture!' },
{ id: 2, author: 'Bending Bender', body: 'Excellent stuff' }
];
Excellent stuff
Great picture!
Returns an array...
}.
}}.
3 comments
1 comments
This is correct...
2 comments
0 comments
2 comments
No comments yet
Level 3
Component State
Level 3
Component State
Handling Data Changes With State
Events
DOM updates
User code does this.
Events
Update state
DOM updates
React does this.
this.state = {
showComments: false
};
}
render() {
...
}
...
}
hides comments
_handleClick() {
this.setState({
showComments: !this.state.showComments
});
}
}
return(
...
<button onClick={this._handleClick.bind(this)}>{buttonText}</button>
...
);
}
...
}
Level 4
Synthetic Events
Level 4
Synthetic Events
Capturing User Actions
Name:
New component
CommentForm
Comment:
CommentBox
CommentForm
Name:
Comment:
Comment
CommentForm
CommentForm
is the same as
function(input) {
this._author = input;
}.bind(this)
}/>
this.props.addComment(author.value, body.value);
}
}
CommentBox
Propagate data about new
comment to CommentBox
Name:
Comment:
CommentForm
CommentBox (Parent)
_addComment(author, body) {
Comment:
}
animate first
CommentBox
onSubmit
eventSubmit
Synthetic event
submitEvent
theSubmitEvent
submitEvent
Quick Recap
We use Reacts event system to
capture user input, including form
submissions and button clicks.
Refs allow us to reference DOM
elements in our code after the
component has been rendered.
Level 5
Level 5 Section 1
Project Folder
index.html
components.js
vendors
react.js
react-dom.js
babel.js
index.html
<!DOCTYPE html>
<html>
<body>
<div id="story-app"></div>
<script src="vendors/react.js"></script>
<script src="vendors/react-dom.js"></script>
<script src="vendors/jquery.js"></script>
<script src="vendors/babel.js"></script>
<script type="text/babel"
src="components.js"></script>
</body>
</html>
jquery.js
Download it from the jQuery website
});
}
}
fetchComments calls
setState, which calls render()
constructor()
componentWillMount()
render()
componentDidMount()
componentWillUnmount()
Requests comments
API
Updates comments
...
class CommentBox extends React.Component {
...
componentDidMount() {
setInterval(() => this._fetchComments(), 5000);
}
}
5,000 milliseconds is
equal to five seconds
No DOM change
Note: render() is called after each Ajax response because setState is in the response function.
Page change
Page change
Store timer as
object property
...
class CommentBox extends React.Component {
...
componentDidMount() {
this._timer = setInterval(
() => this._fetchComments(),
5000
);
}
Run when component is about to be
componentWillUnmount() {
clearInterval(this._timer);
}
}
Page change
Page change
Our component
is smaller again!
Steps 1 2
Steps 3 5
componentWillUnmount() is called
immediately before the component
is removed from the DOM.
Level 5 Section 2
Deleting Comments
Our comments have a Delete Comment button now, but no delete actions are associated to it.
Delete buttons
do not work yet.
}
}
jQuery.ajax({
method: 'DELETE',
url: `/api/comments/${comment.id}`
});
removes comment
from array
CommentBox
Sends this._deleteComment as
argument to child component
CommentBox
Comment
/>
Comment
_handleDelete(event) {
event .preventDefault();
this.props.onDelete(this.props.comment);
}
}
ID should be generated
on the server side
CommentBox
CommentBox
}
}
Pass _deleteComment
as callback
CommentBox
Pass _addComment
as callback
CommentForm
Quick Recap
Heres a review of the two most important things we learned in this section.