The React Beginner's Handbook
The React Beginner's Handbook
The React Beginner's Handbook
org
Table of Contents
Preface
Introduction to React
How much JavaScript you need to know to use
React?
Why should you learn React?
How to install React
React Components
Introduction to JSX
Using JSX to compose UI
The difference between JSX and HTML
Embedding JavaScript in JSX
Managing state in React
Component Props in React
Data flow in a React application
Handling user events in React
Lifecycle events in a React component
Where to go from here
1
Preface
The React Beginner's Handbook follows the 80/20
rule: learn in 20% of the time the 80% of a topic.
Enjoy!
2
www.dbooks.org
Introduction to React
The goal of this handbook is to provide a starter guide
to learning React.
Those topics will be the base upon which you will work
on in other more advanced React courses.
3
You will find some initial difficulties learning React, but
once it "clicks", I guarantee it's going to be one of the
best experiences you will have, because React makes
many things easier than ever, and its ecosystem is
filled with great libraries and tools.
Components
JSX
State
Props
4
www.dbooks.org
How much JavaScript
you need to know to use
React?
Before jumping straight into React, you should have a
good understanding of some core JavaScript
concepts.
Variables
Arrow functions
Work with objects and arrays using Rest and
Spread
Object and array destructuring
Template literals
Classes
Callbacks
Promises
Async/Await
ES Modules
5
Why should you learn
React?
I highly recommend any Web developer to have at
least a basic understanding of React.
6
www.dbooks.org
How to install React
There are a few different ways to install React.
7
outdated version on your system, and every time you
run it, you're getting the latest and greatest code
available.
8
www.dbooks.org
create-react-app created a files structure in the
folder you told ( todolist in this case), and initialized
a Git repository.
file:
9
By default this command launches the app on your
local port 3000, and it opens your browser showing
you the welcome screen:
10
www.dbooks.org
React Components
You just saw how to create your first React application.
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="lo
<p>
Edit <code>src/App.js</code> and save to r
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
)
}
11
An application built using React, or one of the other
popular frontend frameworks like Vue and Svelte for
example, is built using dozens of components.
function App() {
return /* something */
}
12
www.dbooks.org
That is JSX, a special language we use to build a
component's output. We'll talk more about JSX in the
next section.
13
Introduction to JSX
We can't talk about React without first explaining JSX.
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="lo
<p>
Edit <code>src/App.js</code> and save to r
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
)
}
14
www.dbooks.org
We previously ignored everything that was inside the
return statement, and in this section we're going to
talk about it.
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo"
<p>
Edit <code>src/App.js</code> and save to reloa
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
This looks like HTML, but it's not really HTML. It's a
little different.
Under the hood, React will process the JSX and it will
transform it into JavaScript that the browser will be
able to interpret.
15
React gives us this interface for one reason: it's
easier to build UI interfaces using JSX.
In the next section we'll talk about how JSX lets you
easily compose a UI, then we'll look at the differences
with "normal HTML" that you need to know.
16
www.dbooks.org
Using JSX to compose
UI
As introduced in the last section, one of the main
benefits of JSX is to make it very easy to build a UI.
function WelcomeMessage() {
return <p>Welcome!</p>
}
17
Now inside the App component JSX we can add
<WelcomeMessage /> to show this component in the
user interface:
function WelcomeMessage() {
return <p>Welcome!</p>
}
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="lo
<p>
Edit <code>src/App.js</code> and save to r
</p>
<WelcomeMessage />
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
)
}
18
www.dbooks.org
We say WelcomeMessage is a child component of
App, and App is its parent componnet.
19
The difference between
JSX and HTML
JSX kind of looks like HTML, but it's not.
20
www.dbooks.org
React will try its best to make sure things don't break,
but it will raise you a lot of warnings in the Developer
Tools:
This is not the only HTML feature that suffers from this
problem, but it's the most common one.
21
React usually gives very good and informative
error messages that point you in the right direction
to fix the problem.
22
www.dbooks.org
Embedding JavaScript in
JSX
One of the best features of React is that we can easily
embed JavaScript into JSX.
function App() {
const message = 'Hello!'
//...
}
23
We can print this value in the JSX by adding
{message} anywhere in the JSX.
{
message === 'Hello!' ? 'The message was "Hello!"'
}
24
www.dbooks.org
Managing state in React
Every React component can have its own state.
25
useState() accepts the initial value of the state item
and returns an array containing the state variable, and
the function you call to alter the state.
Example:
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Cl
</div>
)
}
26
www.dbooks.org
You can add as many useState() calls you want, to
create as many state variables as you want:
27
Component Props in
React
We call props the initial values passed to a
component.
function WelcomeMessage() {
return <p>Welcome!</p>
}
<WelcomeMessage />
function WelcomeMessage(props) {
return <p>Welcome!</p>
}
28
www.dbooks.org
It's common to use object destructuring to get the
props by name:
29
<WelcomeMessage> Here is some message </WelcomeMessa
30
www.dbooks.org
Data flow in a React
application
In a React application, data typically flows from a
parent component to a child component, using props
as we saw in the previous section:
setCount(1)
//...
}
31
complexity and 90% of the times using those 2 ways I
just explained are the perfect solution.
32
www.dbooks.org
Handling user events in
React
React provides an easy way to manage events fired
from DOM events like clicks, form events and more.
<button
onClick={(event) => {
/* handle the event */
}}
>
Click here
</button>
function App() {
return <button onClick={handleClickEvent}>Click he
}
33
React supports a vast amount of types of events, like
onKeyUp , onFocus , onChange , onMouseDown ,
onSubmit and many more.
34
www.dbooks.org
Lifecycle events in a
React component
So far we've seen how to manage state with the
useState hook.
Here is an example:
35
const { useEffect, useState } = React
useEffect(() => {
console.log(`You clicked ${count} times`)
})
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Cl
</div>
)
}
useEffect(() => {
console.log(`Hi ${name} you clicked ${count} times
}, [name, count])
useEffect(() => {
console.log(`Component mounted`)
}, [])
36
www.dbooks.org
You migth find yourself using this option a lot.
37
Where to go from here
Mastering the topics explained in this book is a great
step towards your goal of learning React.
38
www.dbooks.org
Most of all, make sure you practice by building sample
applications to apply everything you learn.
39