The Road To React
The Road To React
The Road To React
Robin Wieruch
This book is for sale at http://leanpub.com/the-road-to-learn-react
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
FAQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Who is this book for? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
How to read the book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Fundamentals of React . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Hello React . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Setting up a React Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Meet the React Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
React JSX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Lists in React . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Meet another React Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
React Component Instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
React DOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
React Component Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Handler Function in JSX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
React Props . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
React State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Callback Handlers in JSX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Lifting State in React . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
React Controlled Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Props Handling (Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
React Side-Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
React Custom Hooks (Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
React Fragments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Reusable React Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
React Component Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Imperative React . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Inline Handler in JSX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
React Asynchronous Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
React Conditional Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
React Advanced State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
CONTENTS
Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Foreword
The Road to React teaches the fundamentals of React. You will build a real-world application in plain
React without complicated tooling. Everything from project setup to deployment on a server will be
explained to you. The book comes with additional referenced reading material and exercises with
each chapter. After reading the book, you will be able to build your own applications in React. The
material is kept up to date by myself and the community. In the Road to React, I offer a foundation
before you dive into the broader React ecosystem. It explains general concepts, patterns, and best
practices for a real-world React application. Essentially, you will learn to build your own React
application from scratch, with features like pagination, client-side and server-side searching, and
advanced interactions like sorting. I hope this book captures my enthusiasm for React and JavaScript,
and that it helps you get started with it.
Foreword 2
FAQ
How to get updates?
I have two channels where I share updates about my content. You can subscribe to updates by email²
or follow me on Twitter³. Regardless of the channel, my objective is to only share quality content.
Once you receive a notification about an update, you can download a new version of the book from
my website.
Is the learning material up-to-date?
Programming books are usually outdated soon after their release, but since this book is self-
published, I can update it as needed whenever a new version of something related to this book
gets released.
Can I get a digital copy of the book if I bought it on Amazon?
If you have bought the book on Amazon, you may have seen that the book is available on my website
too. Since I use Amazon as one way to monetize my often free content, I honestly thank you for
your support and invite you to sign up for my courses⁴. After creating an account there, write me
an email about your purchase with a receipt from Amazon, so that I can unlock the content for you.
With an account on my platform, you always have access to the latest version of the book.
Why is the print copy so large in size?
If you have purchased the print version of the book, make sure to take notes in the book. It was my
intention to keep the printed book extra-large, for the sake of giving larger code snippets enough
space, but also for giving you enough space to work with it.
Why does the book not have many pages?
First of all, the print version of book is large (see above), so there are less pages overall. However,
even though most of the sections in this book are concisely written, there are plenty of external
resources to get more in-depth knowledge about certain topics. I wanted to give every reader the
chance to get through the book without painfully having to read lots of pages, so I extracted optional
in-depth and advanced material that can be read online as exercise. So I encourage everyone who
wants to learn React to go through the exercises of every section too, however, the book itself should
teach you the essentials and more to get started as well.
Why is the book written like a long read tutorial?
If you have read programming books before, you may be surprised by how this book is written and
structured. When I started coding myself, there were not many hands-on books about coding out
there. However, I’ve been always a good learner when I got practical step by step instructions which
taught me the what, how, and why. By self-publishing this book, I wanted to bring this experience
to other developers.
²https://www.getrevue.co/profile/rwieruch
³https://twitter.com/rwieruch
⁴https://courses.robinwieruch.de/
Foreword 4
Take Breaks
Since every section introduces you to a new topic, it happens fast that you forget the learnings from
the previous section. In addition to coding along with every section, I recommend you to take breaks
between the sections which allow learnings to sink in. Read the section, code along the way, do the
exercise afterwards, code even a bit more if you like, and then rest. Think about your learnings while
taking a walk outside or speak with someone about what you have learned even though this other
person is not into coding. After all, taking breaks is always essential if you want to learn something
new.
Fundamentals of React
In this first part of this learning experience, we’ll cover the fundamentals of React, with which
we’ll create our first React project. Later we’ll explore new use cases for React by implementing real
features like client and server-side searching, remote data fetching, and advanced state management
the same as developing an actual web application. By the end, you will have a fully running React
application that interacts with real-world data.
Fundamentals of React 9
Hello React
Single-page applications (SPA⁷) have become increasingly popular with first-generation SPA frame-
works like Angular (by Google), Ember, Knockout, and Backbone. Using these frameworks made
it easier to build web applications that advanced beyond vanilla JavaScript and jQuery. React, yet
another solution for SPAs, was released by Facebook later in 2013. All of them are used to create
modern web applications in JavaScript.
For a moment, let’s go back in time before SPAs existed: In the past, websites and web applications
were rendered from the server. A user visits a URL in a browser and requests one HTML file and all
its associated HTML, CSS, and JavaScript files from a web server. After some network delay, the user
sees the rendered HTML in the browser (client) and starts to interact with it. Every additional page
transition (meaning: visiting another URL) would initiate this chain of events again. In this version
from the past, essentially everything crucial is done by the server, whereas the client plays a minimal
role by just rendering page by page. While barebones HTML and CSS were used to structure and
style the application, just a little bit of JavaScript was thrown into the mix to make interactions (e.g.
toggling a dropdown) or advanced styling (e.g. positioning a tooltip) possible. A popular JavaScript
library for this kind of work was jQuery.
In contrast, modern JavaScript shifted the focus from the server to the client. The most extreme
version of it: A user visits a URL and requests one small HTML file and one larger JavaScript file.
After some network delay, the user sees the by JavaScript rendered HTML in the browser and starts
to interact with it. Every additional page transition wouldn’t request more files from the web server,
but would use the initially requested JavaScript to render the new page. Also, every additional
interaction by the user is handled on the client too. In this modern version, the server delivers
mainly JavaScript across the wire with one minimal HTML file. The HTML file then executes all the
linked JavaScript on the client-side to render the entire application with HTML and uses JavaScript
for its interactions.
React, among the other SPA solutions, makes this possible. Essentially a SPA is one bulk of JavaScript,
which is neatly organized in folders and files, to create a whole application whereas the SPA
framework (e.g. React) gives you all the tools to architect it. This JavaScript-focused application is
delivered once over the network to your browser when a user visits the URL for your web application.
From there, React, or any other SPA framework, takes over for rendering everything in the browser
as HTML and for dealing with user interactions with JavaScript.
With the rise of React, the concept of components became popular. Every component defines its
look and feel with HTML, CSS, and JavaScript. Once a component is defined, it can be used in a
hierarchy of components for creating an entire application. Even though React has a strong focus
on components as a library, the surrounding ecosystem makes it a flexible framework. React has a
slim API, a stable yet thriving ecosystem, and a welcoming community. We are happy to welcome
you :-)
⁷https://bit.ly/3BZOL1o
Fundamentals of React 10
Exercises
• Read more about how we moved from websites to web applications⁸.
• Optional: Read more about how to learn a framework⁹.
• Optional: Read more about how to learn React¹⁰.
• Read more about JavaScript fundamentals needed for React¹¹ – without worrying too much
about React here – to challenge yourself with several JavaScript features used in React.
• Optional: Leave feedback for this section¹².
⁸https://www.robinwieruch.de/web-applications/
⁹https://www.robinwieruch.de/how-to-learn-framework/
¹⁰https://www.robinwieruch.de/learn-react-js/
¹¹https://www.robinwieruch.de/javascript-fundamentals-react-requirements/
¹²https://forms.gle/NTqhvyDaP1RjtanC6
Fundamentals of React 11
Requirements
To follow this book, you’ll need to be familiar with the basics of web development, i.e how to use
HTML, CSS, and JavaScript. It also helps to understand APIs¹³, as they will be covered in this learning
experience. Along with these skills, you’ll need the following tools to code with me while reading
this book.
¹³https://www.robinwieruch.de/what-is-an-api-javascript/
¹⁴https://www.robinwieruch.de/developer-setup/
¹⁵https://codesandbox.io
¹⁶https://www.robinwieruch.de/git-essential-commands/
¹⁷https://nodejs.org/en/
Fundamentals of React 12
Command Line
node --version
*vXX.YY.ZZ
npm --version
*vXX.YY.ZZ
If you have already installed Node and npm, make sure that your installation is the most recent
version. If you’re new to npm or need a refresher, this npm crash course¹⁸ I created will get you up
to speed.
¹⁸https://www.robinwieruch.de/npm-crash-course/
Fundamentals of React 13
Navigate into your new folder after the setup has finished:
Command Line
cd hacker-stories
Now we can open the application in an editor or IDE. For Visual Studio Code, you can simply type
code . on the command line. The following folder structure, or a variation of it depending on the
create-react-app version, should be presented:
Project Structure
hacker-stories/
--node_modules/
--public/
--src/
----App.css
----App.js
----App.test.js
----index.css
----index.js
----logo.svg
--.gitignore
--package-lock.json
--package.json
--README.md
• README.md: The .md extension indicates the file is a markdown file. Markdown is a
lightweight markup language with plain text formatting syntax. Many projects come with
a README.md file that gives instructions and useful information about the project. When we
push projects to platforms like GitHub, the README.md file usually displays information about
the content contained in its repositories. Because you used create-react-app, your README.md
should be the same as the official’s create-react-app GitHub repository²¹.
• node_modules/: This folder contains all node packages that have been installed. Since we used
create-react-app, a couple of node modules are already installed. We’ll not touch this folder,
since node packages are usually installed and uninstalled with npm via the command line.
• package.json: This file shows you a list of node package dependencies and other project
configurations.
• package-lock.json: This file indicates npm how to break down all node package versions. We’ll
not touch this file.
• .gitignore: This file displays all files and folders that shouldn’t be added to your git repository
when using git, as such files and folders should be located only in your local project. The
node_modules/ folder is one example. It is enough to share the package.json file with others,
so they can install dependencies on their end with npm install without your entire dependency
folder.
• public/: This folder holds development files, such as public/index.html. The index file is
displayed on localhost:3000 when the app is in development or on a domain that is hosted
elsewhere. The default setup handles relating this index.html with all the JavaScript from src/.
In the beginning, everything you need is located in the src/ folder. The main focus lies on the
src/App.js file which is used to implement React components. It will be used to implement your
application, but later you might want to split up your components into multiple files, where each
file maintains one or more components on its own. We will arrive at this point eventually.
Additionally, you will find a src/App.test.js file for your tests, and a src/index.js as an entry point
to the React world. You will get to know both files intimately in later sections. There is also a
src/index.css and a src/App.css file to style your overall application and components, which comes
with the default style when you open them. You will modify them later as well.
After you have learned about the folder and file structure of your React project, let’s go through
the available commands to get it started. All your project-specific commands can be found in your
package.json under the scripts property. They may look similar to these:
²¹https://bit.ly/3jjkzHd
Fundamentals of React 15
package.json
{
...
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
...
}
These scripts are executed with the npm run <script> command in an IDE-integrated terminal or
your standalone command line tool. The run can be omitted for the start and test scripts. The
commands are as follows:
Command Line
Another command from the previous npm scripts called eject shouldn’t be used for this learning
experience. It’s a one-way operation, because once you eject, you can’t go back. Essentially this
command is only there to make all the tooling and configuration from create-react-app accessible if
you are not satisfied with the choices or if you want to change something. Here we will keep all the
defaults though.
Exercises:
• Read a bit more through React’s create-react-app documentation²² and getting started guide²³.
– Read more about the supported JavaScript features in create-react-app²⁴.
• Read more about the folder structure in create-react-app²⁵.
²²https://bit.ly/3jjkzHd
²³https://create-react-app.dev/docs/getting-started
²⁴https://bit.ly/3vvl4Tn
²⁵https://bit.ly/3jeBN8H
Fundamentals of React 16
– Go through all of your React project’s folders and files one by one.
• Read more about the scripts in create-react-app²⁶.
– Start your React application with npm start on the command line and check it out in the
browser.
* Exit the command on the command line by pressing Control + C.
– Run the npm test script.
– Run the npm run build script and verify that a build/ folder was added to your project
(you can remove it afterward). Note that the build folder can be used later on to deploy
your application²⁷.
• Every time we change something in our code throughout the coming learning experience, make
sure to check the output in your browser for getting visual feedback. Use npm start to keep
your application running.
• Optionally: If you use git and GitHub, add and commit your changes after every section of the
book.
• Optional: Leave feedback for this section²⁸.
²⁶https://bit.ly/3vvjsJx
²⁷https://www.robinwieruch.de/deploy-applications-digital-ocean/
²⁸https://forms.gle/bvH2jcppsSA6p9i16
Fundamentals of React 17
function App() {
return (
<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 reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
This file will be our focus throughout this book, unless otherwise specified. Even though this file
will grow in size, it will be simpler to understand as a beginner, because everything is at one place.
Once you get more comfortable with React, I will show you how to split your React project into
multiple files.
Let’s start by reducing the component to a more lightweight version for getting you started without
too much distracting boilerplate code²⁹:
²⁹https://bit.ly/3lZzckS
Fundamentals of React 18
src/App.js
function App() {
return (
<div>
<h1>Hello World</h1>
</div>
);
}
Next, start your application with npm start on the command line and check what’s displayed in the
browser. You should see the headline “Hello React” showing up. Before we dive deeper into each
topic, here comes a quick overview of what’s in your code and what we will cover more in-depth
in the following sections:
• First, this React component, called the App component, is just a JavaScript function. In contrast
to traditional JavaScript functions, it’s defined in PascalCase³⁰. A component has to start with a
capital letter, otherwise it isn’t treated as component in React. The kind of the App component
is commonly called a function component. Function components are the modern way of using
components in React, however, be aware that there are other variations of React components
(see component types in a later section) too.
• Second, the App component doesn’t receive any parameters in its function signature yet. In
the upcoming sections, you will learn how to pass information (see props in a later section)
from one component to another component. These props will be accessible via the function’s
signature as parameters then.
• And third, the App component returns code that resembles HTML. You will see how this new
syntax (see JSX in a later section), allows you to combine JavaScript and HTML for displaying
highly dynamic and interactive content in a browser.
Like any other JavaScript function, a function component can have implementation details between
the function signature and the return statement. You will see this in practice in action throughout
your React journey:
³⁰https://www.robinwieruch.de/javascript-naming-conventions/
Fundamentals of React 19
src/App.js
import * as React from 'react';
function App() {
// you can do something in between
return (
<div>
<h1>Hello World</h1>
</div>
);
}
Variables defined in the function’s body will be re-defined each time this function runs, which
shouldn’t be something new if you are familiar with JavaScript and its functions:
src/App.js
import * as React from 'react';
function App() {
const title = 'React';
return (
<div>
<h1>Hello World</h1>
</div>
);
}
The function of a component runs every time a component is displayed in the browser. This happens
for the initial rendering (read: displaying) of the component, but also whenever the component
updates because it has to display something different due to changes (re-rendering). We will learn
more about this later.
Since we do not want to re-define a variable within a function every time this function runs, we
could define this variable outside of the component as well. In this case, the title does not depend on
any information that’s within the function component (e.g. parameters coming from the function’s
signature), hence it’s okay to move it outside. Therefore it will be defined only once and not every
time the function is called:
Fundamentals of React 20
src/App.js
function App() {
return (
<div>
<h1>Hello World</h1>
</div>
);
}
On your journey as a React developer, and in this learning experience, you will come across both
scenarios: variables (and functions) defined outside and within a component. As a rule of thumb:
If a variable does not need anything from within the function component’s body (e.g. parameters),
then define it outside of the component which avoids re-defining it on every function call.
Exercises:
• Confirm your source code³¹.
• If you are unsure when to use const, let or var in JavaScript (or React) for variable declarations,
read more about their differences³².
• Think about ways to display the title variable in your App component’s returned HTML. In
the next section, we’ll put this variable to use.
• Optional: Leave feedback for this section³³.
³¹https://bit.ly/3G6O6gX
³²https://www.robinwieruch.de/const-let-var/
³³https://forms.gle/VYiZqqjzXGE11wCv6
Fundamentals of React 21
React JSX
Everything returned from a React component will be displayed in the browser. Until now, we only
returned HTML from the App component. However, recall that I mentioned the returned output of
the App component not only resembles HTML, but it can also be mixed with JavaScript. In fact, this
output is called JSX (JavaScript XML), which powerfully combines HTML and JavaScript. Let’s see
how this works for displaying the variable from the previous section:
src/App.js
function App() {
return (
<div>
<h1>Hello {title}</h1>
</div>
);
}
Either start your application again with npm start (or check whether your application still runs) and
look for the rendered (read: displayed) title in the browser. The output should read “Hello React”.
If you change the variable in the source code, the browser should reflect that change.
Changing the variable in the source code and seeing the change reflected in the browser is not solely
connected to React, but also to the underlying development server when we start our application on
the command line. Any time one of our files changes, the development server notices it and reloads
all affected files for the browser. The bridge between React and the development server which makes
this behavior possible is called React Fast Refresh (prior to that it was React Hot Loader) on React’s
side and Hot Module Replacement on the development server’s side.
Next, try to define a HTML input field (read: <input> tag) and a HTML label (read: <label> tag) in
your JSX yourself. It should also be possible to focus the input field when clicking the label either by
nesting the input field in the label or by using dedicated HTML attributes for both. The following
code snippet will show you the book’s implementation of this task and you may be surprised that
HTML slightly differs when used in HTML:
Fundamentals of React 22
src/App.js
function App() {
return (
<div>
<h1>Hello {title}</h1>
For our input field and label combination, we specified three HTML attributes: htmlFor, id, and
type. The type attribute is kinda mandatory and has nothing to do with focusing the input field
when clicking the label. However, while id and type should be familiar from native HTML, htmlFor
might be new to you.
The htmlFor reflects the for attribute in vanilla HTML. You may be wondering why this attribute
differs from native HTML. JSX replaces a handful of internal HTML attributes caused by internal
implementation details of React itself. However, you can find all the supported HTML attributes³⁴ in
React’s documentation. Since JSX is closer to JavaScript than to HTML, React uses the camelCase³⁵
naming convention. Expect to come across more JSX-specific attributes like className and onClick
instead of class and onclick, as you learn more about React.
When using HTML in JSX, React internally translates all HTML attributes to JavaScript where
certain words such as class or for are reserved during the rendering process. Therefore React came
up with replacements such as className and htmlFor for them. However, once the actual HTML is
rendered for the browser, the attributes get translated back to their native variant.
³⁴https://bit.ly/2Z42zcK
³⁵https://bit.ly/3jljQFn
Fundamentals of React 23
We will revisit the HTML input field and its label for further implementation details with JavaScript
later. For now, in order to contrast how HTML and JavaScript are used in JSX, let’s use more complex
JavaScript daya types in JSX. Instead of defining a JavaScript string primitive like title, define a
JavaScript object called welcome which has a title (e.g. 'React') and a greeting (e.g. 'Hey') as
properties. Afterward, try yourself to render both properties of the object in JSX side by side in the
<h1> tag.
The following code snippet will show you the solution to the task. Before we have defined a
JavaScript string primitive to be displayed in the App component. Now, the same can be done with
a JavaScript object by accessing its properties within JSX:
src/App.js
const welcome = {
greeting: 'Hey',
title: 'React',
};
function App() {
return (
<div>
<h1>
{welcome.greeting} {welcome.title}
</h1>
While HTML can be used almost (except for the attributes) in its native way in JSX, everything in
curly braces can be used to interpolate JavaScript in it. For example, you could define a function
that returns the title and execute it within the curly braces:
src/App.js
function getTitle(title) {
return title;
}
function App() {
return (
<div>
<h1>Hello {getTitle('React')}</h1>
JSX is a syntax extension to JavaScript. In the past, JavaScript files which made use of JSX had
to use³⁶ the .jsx instead of the .js extension. However, these days the underlying build tools (read:
compiler/bundler) can be configured to acknowledge JSX in a .js file³⁷. If they are configured this
way, they will transpile JSX to JavaScript. Tools like create-react-app take care of this for us, because
they have all the internal build tools (e.g. Webpack, Babel) in place to make this happen. So you
don’t need to worry about these kind of setup related topics as a beginner.
³⁶https://github.com/airbnb/javascript/pull/985
³⁷https://www.robinwieruch.de/minimal-react-webpack-babel-setup/
Fundamentals of React 25
Code Playground
const title = 'React';
// JSX ...
const myElement = <h1>Hello {title}</h1>;
JSX enables developers to express what should be rendered by mixing up HTML with JavaScript.
Whereas the previous way of thinking was to decouple markup (read: HTML) from logic (read:
JavaScript), React puts all of it together as one unit in a React component. As you can see from the
last code snippet, React does not require you to use JSX at all, instead it’s possible to use methods
like createElement(). However, most people find it more intuitive to use JSX for its declarative
nature instead of using JavaScript methods (here: methods offered by React) which only allow one
to express the UI imperatively.
Initially invented for React, JSX gained popularity in other modern libraries and frameworks as well.
These days, it’s not strictly coupled to React, but people are usually connecting it to React. Anyway,
JSX is one of my favorite things when being asked about React³⁸. Without any extra templating
syntax (except for the curly braces), we are able to use JavaScript in HTML. Every JavaScript data
structure, from primitive to complex, can be used within HTML with the help of JSX.
Exercises:
• Confirm your source code³⁹.
– Confirm the changes⁴⁰.
• Beginner: Read more about JavaScript Variables⁴¹.
– Beginner: Define more primitive and complex JavaScript data types and render them in
JSX.
– Advanced: Try to render a JavaScript array in JSX by using the array’s built-in map()
method to return JSX for each item in the list. If it’s too complicated, don’t worry, because
you will learn more about this in the next section.
• Optional: Read more about React’s JSX⁴².
• Optional: Leave feedback for this section⁴³.
³⁸https://bit.ly/3aZbdM0
³⁹https://bit.ly/3vvS8ec
⁴⁰https://bit.ly/3n3WW6o
⁴¹https://www.robinwieruch.de/javascript-variable/
⁴²https://bit.ly/3BZSkVk
⁴³https://forms.gle/R6y6kEqGPACLrXmP8
Fundamentals of React 26
Lists in React
When working with data in JavaScript, most often the data comes as an array of objects. Therefore,
we will learn how to render a list of items in React next. In order to prepare you for rendering lists
in React, let’s recap one of the most common data manipulation methods: the array’s built-in map()
method⁴⁴. It is used to iterate over each item of a list in order to return a new version of each item:
Code Playground
const numbers = [1, 2, 3, 4];
console.log(exponentialNumbers);
// [1, 4, 9, 16]
In React, the array’s built-in map() method is used to transform a list of items into JSX by returning
JSX for each item. In the following, we want to display a list of items (here: JavaScript objects) in
React. First, we will define the array outside of the component. Afterward, try yourself to render
each object with its title property in React by inlining the array’s map() method in JSX:
src/App.js
import * as React from 'react';
const list = [
{
title: 'React',
url: 'https://reactjs.org/',
author: 'Jordan Walke',
num_comments: 3,
points: 4,
objectID: 0,
},
{
title: 'Redux',
url: 'https://redux.js.org/',
author: 'Dan Abramov, Andrew Clark',
num_comments: 2,
points: 5,
objectID: 1,
⁴⁴https://mzl.la/3B3a7tf
Fundamentals of React 27
},
];
Each item in the list has a title, an url, an author, an identifier (objectID), points – which indicate
the popularity of an item – and a count of comments (num_comments). The property names are chosen
this way, because they resemble real world data that we are going to use later. They don’t fit the
desired naming conventions⁴⁵ for JavaScript though.
Next, we’ll render the list inlined in JSX with the array’s built-in map() method. Hence we won’t
map from one JavaScript data type to another, but instead return JSX that renders each item of the
list:
src/App.js
function App() {
return (
<div>
<h1>My Hacker Stories</h1>
<hr />
<ul>
{list.map(function (item) {
return <li>{item.title}</li>;
})}
</ul>
</div>
);
}
Actually, rendering a list of items in React was one of my personal JSX “Aha” moments. Without
⁴⁵https://www.robinwieruch.de/javascript-naming-conventions/
Fundamentals of React 28
any made up templating syntax, it’s possible to use JavaScript to map from a list of items to a list of
HTML elements. That’s what JSX is for the developer in the end: just JS mixed with HTML.
Finally React displays each item now. But there is one important piece missing. If you check your
browser’s developer tools, you should see a warning showing up in the “Console”-tab which says
that every React element in a list should have a key assigned to it. The key is an HTML attribute
and should be a stable identifier. Fortunately, our items come with such a stable identifier, because
they have an id (here: objectId):
src/App.js
function App() {
return (
<div>
...
<ul>
{list.map(function (item) {
return <li key={item.objectID}>{item.title}</li>;
})}
</ul>
</div>
);
}
The key attribute is used for one specific reason: Whenever React has to re-render a list, it checks
whether an item has changed. When using keys, React can efficiently exchange the changed items.
When not using keys, React may update the list inefficiently. Take the following example where a
new item gets appended at the start of the list.
Fundamentals of React 29
The key is not difficult to find, because usually when having data in shape of an array, we can use
each item’s stable identifier. However, sometimes you do not have an id, so you need to come up
with another identifier (e.g. title if it does not change and if it’s unique in the array). As last resort,
you can use the index of the item in the list too:
Code Playground
<ul>
{list.map(function (item, index) {
return (
<li key={index}>
{/* only use an index as last resort */}
{/* and by the way: that's how you do comments in JSX */}
{item.title}
</li>
);
})}
</ul>
Usually using an index should be avoided though, because it comes with the same rendering
performance issues from above. In addition, it can cause actual bugs in the UI⁴⁶ whenever the order
of items got changed (e.g. re-ordering, appending or removing items). However, as last resort, if the
list does not change its order in any way, using the index is fine.
So far, we are only displaying the title of each item. Go ahead and render the item’s url, author,
num_comments, and points as well. In the special case of the url, use an HTML anchor element (read:
<a> tag) that surrounds the title. For guidance, the following solution will show you how the book
implements this to be prepared for the next sections:
⁴⁶https://www.robinwieruch.de/react-list-key/
Fundamentals of React 30
src/App.js
function App() {
return (
<div>
...
<ul>
{list.map(function (item) {
return (
<li key={item.objectID}>
<span>
<a href={item.url}>{item.title}</a>
</span>
<span>{item.author}</span>
<span>{item.num_comments}</span>
<span>{item.points}</span>
</li>
);
})}
</ul>
</div>
);
}
The array’s map() method is inlined concisely in your JSX for rendering a list. Within the map()
method, we have access to each object and its properties. The url property of each item is used
as href attribute for the HTML anchor element. Not only can JavaScript in JSX be used to display
elements, but also to assign HTML attributes dynamically. This section only scratches the surface
of how powerful it is to mix JavaScript and HTML, however, using an array’s map() method and
assigning HTML attributes should give you a good first impression.
Exercises:
• Confirm your source code⁴⁷.
– Confirm the changes⁴⁸.
• Recap the standard built-in array methods⁴⁹, especially map, filter, and reduce, which are
available in native JavaScript.
• Question: What happens if you return null instead of the JSX?
– Answer: Returning null in JSX is allowed. It’s always used if you want to render nothing.
⁴⁷https://bit.ly/2Z6e2ZI
⁴⁸https://bit.ly/3jf7a2Q
⁴⁹https://mzl.la/3b9V9rf
Fundamentals of React 31
• Extend the list with some more items to make the example more realistic.
• Practice using different JavaScript expressions in JSX.
• Optional: Leave feedback for this section⁵⁰.
⁵⁰https://forms.gle/aZmLFjEdSMTk9Thk9
Fundamentals of React 32
function List() {
return (
<ul>
{list.map(function (item) {
return (
<li key={item.objectID}>
<span>
<a href={item.url}>{item.title}</a>
</span>
<span>{item.author}</span>
<span>{item.num_comments}</span>
<span>{item.points}</span>
</li>
);
})}
</ul>
);
}
Then the new List component can be used in the App component where we have been using the
inlined HTML elements previously:
Fundamentals of React 33
src/App.js
function App() {
return (
<div>
<h1>My Hacker Stories</h1>
<hr />
<List />
</div>
);
}
You’ve just created your first React component. With this example in mind, we can see how
components encapsulate meaningful tasks while contributing to the greater good of a larger React
project. Extracting a component is a task that you will perform very often as a React developer,
because it’s always the case that a component will grow in size and complexity. Go ahead and
extract the label and input elements from the App component into their own Search component.
The following code snippet shows how the book would solve this task:
src/App.js
function App() {
return (
<div>
<h1>My Hacker Stories</h1>
<Search />
<hr />
<List />
</div>
);
}
function Search() {
return (
<div>
<label htmlFor="search">Search: </label>
Fundamentals of React 34
Finally, we have three components in our application: App, List, and Search. Generally speaking, a
React application consists of many hierarchical components; which we can put into the following
categories. The following illustration assumes that we have split out an Item component from the
List component as well – which helps us to clarify the taxonomie.
React applications have component hierarchies (also called component trees). There is usually one
uppermost entry point component (e.g. App) that spans a tree of components below it. The App
is the parent component of the List and Search, so the List and Search are child components of
the App component and sibling components to each other. The illustration takes it one step further
where the Item component is a child component of the List. In a component tree, there is always a
root component (e.g. App), and the components that don’t render any other components are called
leaf components (e.g. List/Search component in our current source code or Item/Search component
from the illustration). All components can have zero, one, or many child components.
You can see how a React application grows in size by creating more components which are connected
in one hierarchy. Usually you will start out with the App component from where you grow your
component tree. Either you know the components you wanna create beforehand or you start with
one component and extract components from it eventually. For a beginner, it may be difficult to
know when to create a new component or when to extract a component from another component.
Usually it happens naturally whenever a component gets too big in size/complexity or whenever
you see natural boundaries in domains/functionality (e.g. List component renders a list of items,
Search component renders a search form). In the end, each component represents a single unit in
your application which makes the application maintainable and predictable.
Fundamentals of React 35
Exercises:
• Confirm your source code⁵¹.
– Confirm the changes⁵².
• We can’t extract an Item component from the List component (like in the illustration) yet,
because we don’t know how to pass individual items from the list to each Item component.
Think about a way to do it.
• Optional: Leave feedback for this section⁵³.
⁵¹https://bit.ly/3plb66a
⁵²https://bit.ly/3G61kKU
⁵³https://forms.gle/EZENmy48zvDP82NL7
Fundamentals of React 36
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
getName() {
return this.firstName + ' ' + this.lastName;
}
}
Each class has a constructor that takes arguments and assigns them to the class instance when
instantiating it. A class can also define functions that are associated with the instance (e.g. getName())
which are called methods or class methods. Now, declaring the Person class once is just one part;
instantiating it is the other. The class declaration is the blueprint of its capabilities and usage occurs
when an instance is created with the new statement. If a JavaScript class declaration exists, one can
create multiple instances of it:
Code Playground
// class declaration
class Person { ... }
// class instantiation
const robin = new Person('Robin', 'Wieruch');
console.log(robin.getName());
// "Robin Wieruch"
Fundamentals of React 37
console.log(dennis.getName());
// "Dennis Wieruch"
The concept of a class with declaration and instantiation is similar to a React component, which also
has only one component declaration, but can have multiple component instances:
src/App.js
// declaration of App component
function App() {
return (
<div>
...
Once we’ve defined a component, we can use it as an element anywhere in our JSX. The element
produces a component instance of your component, or in other words, the component gets
instantiated. You can create as many component instances as you want as long as you have a
component declaration. It’s not much different from a JavaScript class declaration and instantiation,
however, as mentioned before, technically a JavaScript class and React component are not the same.
Just their usage makes it convenient to demonstrate their similarities.
Exercises:
• Familiarize yourself with the terms component declaration, component instance, and element.
• Experiment by creating multiple component instances of a List component.
• If we keep treating the list variable as a global variable, every List component would use the
same list. Think about how it could be possible to give each List component its own list
variable.
Fundamentals of React 38
⁵⁴https://forms.gle/sf1UMNR58v3NsRUSA
Fundamentals of React 39
React DOM
We have learned about component declaration/instantiation and have already seen it in action for
the List and Search components. However, at the very beginning we started with the declaration
of the App component yet never came across its instantiation. It must be there, otherwise the App
component and all of its descendant components in the component hierarchy would not render.
Open the src/index.js file to the see App components instantiation with the <App /> element. The
file may differ a bit from your file, however, the following snippet shows all the essential aspects of
it:
src/index.js
There are two libraries imported at the beginning of the file: react and react-dom. While React is
used for the day to day business of a React developer, React DOM is usually used once in a React
application to hook React into the native HTML world. Open the public/index.html file on the side
and spot the HTML element where the id attribute equals "root". That’s exactly the element where
React inserts itself into the HTML to bootstrap the entire React application – starting with the App
component.
In the JavaScript file, the createRoot() method expects the HTML element that is used to instantiate
React. There we are using JavaScript’s built-in getElementById() method to return the HTML
element that we have seen in the public/index.html file. Once we have the root object, we can call its
render() method with JSX as parameter which usually represents the entry point component (also
called root component). Normally the entry point component is the instance of the App component,
but it can be any other JSX too:
Fundamentals of React 40
Code Playground
Essentially React DOM is everything that’s needed to integrate React into any website which uses
HTML. If you start a React application from scratch, there is usually only one ReactDOM.createRoot()
call in your application. However, if you happen to work on a legacy application that used something
else than React before, you may see multiple ReactDOM.createRoot() calls, because only certain
areas of the application are powered by React.
Anyway, do you recall the introduction about the rise of single-page applications that are powered
by only a small HTML file and a large JavaScript file? You can see how everything fits together
now. While one small HTML file (here: public/index.html) and one large JavaScript file (here:
compiled and bundled src/index.js and src/App.js files) are transferred from web server to browser,
the JavaScript file(s) are mostly responsible to render all the HTML in the browser. The HTML file
is only there to request the JavaScript file and to render the HTML element where React inserts
itself. From there, React calls all of its needed function components to render itself as component
hierarchy.
Exercises:
• Read more about rendering elements in React⁵⁵.
• Optional: Leave feedback for this section⁵⁶.
⁵⁵https://bit.ly/3aUySgP
⁵⁶https://forms.gle/zSqHUhmsuQ35vqoj9
Fundamentals of React 41
// function declaration
function App() { ... }
Equipped with this knowledge, go through your React project and refactor all function declarations
to arrow function expressions. While this refactoring can be applied to function components, it can
also be used for any other functions that are used in the project. In the book, we will go ahead as
well and refactor all the function component’s function declarations to arrow function expressions:
src/App.js
As said, not only function components can be refactored, but also other functions like the callback
function⁵⁷ that we have used for the array’s map() method:
⁵⁷https://www.robinwieruch.de/javascript-callback-function/
Fundamentals of React 42
src/App.js
If an arrow function’s only purpose is to return a value and it doesn’t have any business logic in
between, you can remove the block body (curly braces) of the function. In a concise body, an
implicit return statement is attached, so you can remove the return statement:
Code Playground
return count + 1;
};
This can be done for the App, List, and Search components as well, because they only return JSX
and don’t perform any task in between. In addition, it also applies to the arrow function that’s used
in the map() method:
Fundamentals of React 43
src/App.js
const App = () => (
<div>
...
</div>
);
All JSX is more concise now, because it omits the function statement, the curly braces, and the return
statement. However, it’s important to remember this is an optional step and that it’s acceptable to
use function declarations over arrow function expressions and block bodies with curly braces over
concise bodies with implicit returns for arrow functions.
Often block bodies will be necessary to introduce more business logic between function signature
and return statement. Be sure to understand this refactoring concept, because we’ll move quickly
from arrow function components with and without block bodies as we go. Which one we use will
depend on the requirements of the component:
Code Playground
const App = () => {
// perform a task in between
return (
<div>
...
</div>
);
};
Fundamentals of React 44
As a rule of thumb, use either function declarations or arrow function expressions for your
component declarations throughout your application. Both versions are fine to use, but make sure
that you and your team working on the project share the same implementation style. In addition,
while an implicit return statement when using an arrow function expressions makes your component
declaration more concise, you may introduce tedious refactorings from concise to block body when
you need to perform tasks between function signature and the return statement. So you may want
to keep your arrow function expression with a block body (like in the last code snippet) all the time.
Exercises:
• Confirm your source code⁵⁸.
– Confirm the changes⁵⁹.
• Optional: Read more about JavaScript arrow functions⁶⁰.
• Familiarize yourself with arrow functions with block body and explicit return and concise body
without return (implicit return).
• Optional: Leave feedback for this section⁶¹.
⁵⁸https://bit.ly/2ZbLXQz
⁵⁹https://bit.ly/3pkH2aS
⁶⁰https://mzl.la/3BYCOcp
⁶¹https://forms.gle/iWSchmqasbZUWUpT8
Fundamentals of React 45
return (
<div>
<label htmlFor="search">Search: </label>
<input id="search" type="text" />
</div>
);
};
Next, define a function, which can be either a function declaration or arrow function expression, for
the change event of the input field. In React, this function is called an (event) handler. Afterward,
the function can be passed to the onChange attribute (JSX named attribute) of the HTML input field:
src/App.js
return (
<div>
<label htmlFor="search">Search: </label>
<input id="search" type="text" onChange={handleChange} />
⁶²https://mzl.la/2ZbTcYZ
Fundamentals of React 46
</div>
);
};
After opening your application in a web browser, open the browser’s developer tools “Console”-tab
to see the logging occur after you type into the input field. What you see is called a synthetic event
as a JavaScript object and the input field’s internal value.
React’s synthetic event is essentially a wrapper around the browser’s native event⁶³. Since React
started as library for single-page applications, there was the need for enhanced functionalities on
the event to prevent the native browser behavior⁶⁴. For example, in native HTML submitting a
form triggers a page refresh. However, in React this page refresh should be prevented, because the
developer should take care about what happens next. Anyway, if you happen to need access to the
native HTML event, you could do so by using event.nativeEvent, but after several years of React
development I never ran into this case myself.
After all, this is how we give HTML elements in JSX handler functions to add listeners for user
interactions. Always pass functions to these handlers, not the return value of the function, except
when the return value is a function again. Knowing this is crucial because it’s a well-known source
for bugs in a React beginner’s application:
Code Playground
// if handleChange is a function
// which does not return a function
// don't do this
<input onChange={handleChange()} />
// do this instead
<input onChange={handleChange} />
As you can see, HTML and JavaScript work well together in JSX. JavaScript in HTML can display
JavaScript variables (e.g. title string in <span>{title}</span>), can pass JavaScript primitives to
HTML attributes (e.g. url string to <a href={url}> HTML element), and can pass functions to
an HTML element’s attributes for handling user interactions (e.g. handleChange function to <input
onChange={handleChange} />). When developing React applications, mixing HTML and JavaScript
in JSX will become your bread and butter.
Exercises:
• Confirm your source code⁶⁵.
⁶³https://mzl.la/30Dk8kt
⁶⁴https://www.robinwieruch.de/react-preventdefault/
⁶⁵https://bit.ly/3lY8usB
Fundamentals of React 47
⁶⁶https://bit.ly/3BYqQzp
⁶⁷https://www.robinwieruch.de/react-event-handler/
⁶⁸https://bit.ly/3jiFdaz
⁶⁹https://www.robinwieruch.de/react-event-bubbling-capturing/
⁷⁰https://forms.gle/oSKyMudmb8X1iSsv8
Fundamentals of React 48
React Props
Currently we are using the list as a global variable in our project. At the beginning, we used it
directly from the global scope in the App component and later in the List component. This could
work if you only had one global variable, but it isn’t maintainable with multiple variables across
multiple components (within multiple folders/files). By using so-called props in React, we can pass
variables as information from one component to another component. Let’s explore how this works.
Before using props for the first time, we’ll move the list from the global scope into the App
component and give it a self-descriptive name. Don’t forget to refactor the App component’s function
from concise to block body in order to declare the list prior to the return statement:
src/App.js
return ( ... );
};
Next, we’ll use React props to pass the list of items to the List component. The variable is called
stories in the App component and we pass it under this name to the List component. However, in
the List component’s instantiation, it is assigned to the list HTML attribute:
Fundamentals of React 49
src/App.js
const App = () => {
const stories = [ ... ];
return (
<div>
...
Now try yourself to retrieve the list from the List component’s function signature by using
introducing a parameter. If you find the solution yourself, congratulations for passing your first
information from one component to another. If not, the following code snippet shows how it works:
src/App.js
const List = (props) => (
<ul>
{props.list.map((item) => (
<li key={item.objectID}>
...
</li>
))}
</ul>
);
Everything that we pass from a parent component to a child component via the component element’s
HTML attribute can be accessed in the child component. The child component receives a parameter
(props) as object in its function signature which includes all the passed attributes as properties (short:
props).
Note that at this point, we could also define stories directly in the List component and would not
need to pass them as props, however, in the future we will make use of the stories in the App
component and thus will keep them there. In addition, this was a great learning exercise to get to
know props in React.
Another use case for React props is the List component and its potential child component. Previously,
we couldn’t extract an Item component from the List component, because we didn’t know how to
pass each item to the extracted Item component. With this new knowledge about React props, we
can perform the component extraction and pass each item along to the List component’s new child
component.
Fundamentals of React 50
Before you check the following solution, try it yourself: extract an Item component from the List
component and pass each item in the map() methods callback function to this new component. If
you don’t come up with a solution yourself after some time, check out how the books implements
it:
src/App.js
The most important fact about props: it’s not allowed to change them, because they should be
treated as an immutable data structure. They are only used to pass information down the component
hierarchy. Continuing this thought, information (props) can only be passed from a parent to a child
component and not vice versa. We will learn how to overcome this limitation later. For now, we
have found our vehicle to share information from top to bottom in a React component tree.
Exercises:
• Confirm your source code⁷¹.
– Confirm the changes⁷².
• Read more about how to use props in React⁷³.
• Optional: Leave feedback for this section⁷⁴.
⁷¹https://bit.ly/3jlmUBz
⁷²https://bit.ly/3lW2sIX
⁷³https://www.robinwieruch.de/react-pass-props-to-component/
⁷⁴https://forms.gle/APwaUSAuVAAA56sY6
Fundamentals of React 51
React State
While it is not allowed to mutate React props as a developer, because they are only there to pass
information from parent to child components, React state introduces a mutable data structure (read:
stateful values). These stateful values get instantiated in a React component as so called state, can
be passed with props as vehicle down to child components, but can also get mutated by using a
function to modify the state. When a state gets mutated, the component with the state and all child
components will re-render.
Both concepts, props and state, have clear defined purposes: While props are used to pass information
down the component hierarchy, state is used to change information over time. Let’s start with state
in React with the following use case: Whenever a user types text into our HTML input field element
in the Search component, the user wants to see this information (state) displayed next to it. An
intuitive (but not working) approach would be the following:
Fundamentals of React 52
src/App.js
return (
<div>
<label htmlFor="search">Search: </label>
<input id="search" type="text" onChange={handleChange} />
<p>
Searching for <strong>{searchTerm}</strong>.
</p>
</div>
);
};
When you try this in the browser, you will see that the output does not appear below the HTML
input field after typing into it. However, this approach is not too far away from the actual solution.
What’s missing is telling React that searchTerm is a stateful value. Fortunately, React offers us a
method called useState for it:
src/App.js
...
};
By using useState, we are telling React that we want to have a stateful value which changes over
time. And whenever this stateful value changes, the affected components (here: Search component)
will re-render to use it (here: to display the recent value).
React’s useState method takes an initial state as an argument – in our case it is an empty string.
Furthermore, calling this method will return an array with two entries: The first entry (searchTerm)
Fundamentals of React 53
represents the current state. The second entry (setSearchTerm) is a function to update this state.
The book will refer to this function as state updater function. Both entries are everything we need
to display the current state and to update it.
When the user types into the input field, the input field’s change event runs into the event handler.
The handler’s logic uses the event’s value of the target and the state updater function to set the
updated state. Afterward, the component re-renders (read: the component function runs). The
updated state becomes the current state (here: searchTerm) and is displayed in the component’s
JSX.
As an exercise, put a console.log() into each of your components. For example, the App component
gets a console.log('App renders'), the List component gets a console.log('List renders') and
so on. Now check your browser: For the first rendering, all loggings should appear, however, once
you type into the HTML input field, only the Search component’s logging should appear. React
only re-renders this component (and all of its potential descendant components) after its state has
changed.
Now you have heard the terms rendering and re-rendering in a technical context as well. In essence
every component in a React application has one initial rendering followed by potential re-renderings.
Usually the initial rendering happens when a React component gets displayed in the browser. Then
whenever a side-effect occurs, like a user interaction (e.g. typing into an input field), the change is
captured in React’s state which forces a re-rendering of all the components affected by this change;
meaning the component which manages the state and all its descendant components.
It’s important to note that the useState function is called a React hook. It’s only one of many hooks
provided by React and this section only scratched the surface of hooks in React. You will learn more
about them throughout the next sections. As for now, you should know that you can have as many
useState hooks as you want in one or multiple components whereas state can be anything from a
Fundamentals of React 54
JavaScript string (like in this case) to a more complex data structure such as an array or object.
When the UI is rendered for the first time, every useState hook gets initialized with an initial state
which gets returned as current state. Whenever the UI is re-rendered because of a state change,
the useState hook uses the most recent state from its internal closure⁷⁵. This might seem odd, as
one would assume the useState gets declared from scratch every time a component’s function runs.
However, next to each component React allocates an invisible container where information like state
is stored in memory.
Exercises:
• Confirm your source code⁷⁶.
– Confirm the changes⁷⁷.
• Optional: Read more about JavaScript array destructuring⁷⁸.
• Read more about React’s useState Hook⁷⁹.
• Optional: Leave feedback for this section⁸⁰.
⁷⁵https://www.robinwieruch.de/javascript-closure/
⁷⁶https://bit.ly/3prVjSO
⁷⁷https://bit.ly/30ISOBv
⁷⁸https://mzl.la/3ncC7WI
⁷⁹https://www.robinwieruch.de/react-usestate-hook/
⁸⁰https://forms.gle/ZJNbQqq3Lw3RiD4H9
Fundamentals of React 55
There is no way to pass information up the component tree, since props are naturally only passed
downwards. However, we can introduce a callback handler instead: A callback handler gets
introduced as event handler (A), is passed as function in props to another component (B), is executed
there as callback handler (C), and calls back to the place it was introduced (D):
src/App.js
// A
const handleSearch = (event) => {
// D
console.log(event.target.value);
};
return (
Fundamentals of React 56
<div>
<h1>My Hacker Stories</h1>
{/* // B */}
<Search onSearch={handleSearch} />
<hr />
// C
props.onSearch(event);
};
return ( ... );
};
Whenever a user types into the input field now, the function that is passed down from the App
component to the Search component runs. This way, we can notify the App component when a
user types into the input field in the Search component. Essential a callback handler, which is just a
more specific type of an event handler, becomes our implicit vehicle to communicate upwards the
component tree.
Fundamentals of React 57
The concept of the callback handler in a nutshell: We pass a function from a parent component (App)
to a child component (Search) via props; we call this function in the child component (Search), but
have the actual implementation of the called function in the parent component (App). Essentially
when an (event) handler is passed as props from a parent component to its child component, it
becomes a callback handler. React props are always passed down the component tree and therefore
functions that are passed down as callback handlers in props can be used to communicate up the
component hierarchy.
Exercises:
• Confirm your source code⁸¹.
– Confirm the changes⁸².
• Revisit the concepts of (event) handler and callback handler as many times as you need.
• Optional: Leave feedback for this section⁸³.
⁸¹https://bit.ly/3DUWm1O
⁸²https://bit.ly/3jizj9s
⁸³https://forms.gle/3LoBoWKCMNT2YpnA7
Fundamentals of React 58
return (
<div>
<h1>My Hacker Stories</h1>
<hr />
};
We learned about the callback handler previously, because it helps us to keep an open communi-
cation channel from child component (here: Search component) to parent component (here: App
component). Now, the Search component doesn’t manage the state anymore, but only passes up the
event to the App component via a callback handler after the text is entered into the HTML input field.
From there, the App component updates its state. The process of moving state from one component
to another, like we did in the last code snippet, is called lifting state. Next, you could still display the
searchTerm again in the App component (from state, when using searchTerm) or Search component
(from props, when passing the searchTerm state down as props).
Rule of thumb: Always manage state at a component level where every component that’s interested
in it is one that either manages the state (using information directly from state, e.g. App component)
or a component below the state managing component (using information from props, e.g. List or
Search components). If a component below needs to update the state (e.g. Search), pass a callback
handler down to it which allows this particular component to update the state above in the parent
component. If a component below needs to use the state (e.g. displaying it), pass it down as props.
Finally, by managing the search state in the App component, we can filter the stories with the
stateful searchTerm before passing them as list prop to the List component. Try it yourself by
using the array’s built-in filter() method in combination with the stories and the searchTerm
before consulting the following implementation:
Fundamentals of React 60
src/App.js
return (
<div>
<h1>My Hacker Stories</h1>
<hr />
Here, the JavaScript array’s built-in filter method⁸⁴ is used to create a new filtered array. The
filter() method takes a function as an argument, which accesses each item in the array and returns
true or false. If the function returns true, meaning the condition is met, the item stays in the newly
created array; if the function returns false, it’s removed:
⁸⁴https://mzl.la/3BYFAOR
Fundamentals of React 61
Code Playground
const words = [
'spray',
'limit',
'elite',
'exuberant',
'destruction',
'present'
];
console.log(filteredWords);
// ["exuberant", "destruction", "present"]
The filter() method could be made more concise by using an arrow function with an immediate
return:
src/App.js
...
};
That’s all to the refactoring steps of the inlined function for the filter() method. There are many
variations to it – and it’s not always simple to keep a good balance between readable and conciseness
– however, I feel like keeping it concise whenever possible keeps it most of the time readable as well.
What’s not working very well yet: The filter() method checks whether the searchTerm is present
as string in the title property of each story, but it’s case sensitive. If we search for “react”, there is no
filtered “React” story in your rendered list. Try to fix this problem yourself by making the filter()
method’s condition case insensitive. The following code snippet shows you how to achieve it by
lower casing the searchTerm and the title of the story:
Fundamentals of React 62
src/App.js
...
};
Now you should be able to search for “eact”, “React”, or “react” and see one of two displayed
stories. Congratulations, you have just added your first real interactive feature to your application
by leveraging state – to derive a filtered list of stories – and props – by passing a callback handler
to the Search component.
After all, knowing where to instantiate state in React turns out to be an important skill in every React
developer’s career. The state should always be there where all components which depend on the state
can read (via props) and update (via callback handler) it. These are all descendant components of
the component which instantiates the state.
Fundamentals of React 63
Exercises:
• Confirm your source code⁸⁵.
– Confirm the changes⁸⁶.
• Read more about lifting state in React⁸⁷.
• Optional: Leave feedback for this section⁸⁸.
⁸⁵https://bit.ly/3vtfBwo
⁸⁶https://bit.ly/3DSiuK6
⁸⁷https://www.robinwieruch.de/react-lift-state/
⁸⁸https://forms.gle/EqJGjxCM1Xzw9S6g7
Fundamentals of React 64
...
return (
<div>
<h1>My Hacker Stories</h1>
...
</div>
);
};
id="search"
type="text"
value={props.search}
onChange={props.onSearch}
/>
</div>
);
Now both states are synchronized. Instead of giving the HTML element the freedom of keeping
track of its internal state, it uses React’s state by leveraging the element’s value attribute instead.
Whenever the HTML element emits a change event, the new value is written to Reacts state and
re-renders the components. Then the HTML element uses the recent state as value again.
Earlier the HTML element did its own thing, but now we are in control of it by feeding React’s state
into it. Now, while the input field became explicitly a controlled element, the Search component
became implicitly a controlled component. As a React beginner, using controlled components is
important, because you want to enforce a predictable behavior. Later though, there will be cases for
uncontrolled components too.
Exercises:
• Confirm your source code⁸⁹.
– Confirm the changes⁹⁰.
• Read more about controlled components in React⁹¹.
• Optional: Leave feedback for this section⁹².
⁸⁹https://bit.ly/3aXr7GZ
⁹⁰https://bit.ly/3aV4XVO
⁹¹https://www.robinwieruch.de/react-controlled-components/
⁹²https://forms.gle/7VYTww2EQiPkFnaR8
Fundamentals of React 66
The following refactorings are recommended for you to learn different JavaScript/React patterns,
though you can still build complete React applications without them. Consider these as advanced
props techniques that will make your React code for certain scenarios more concise, readable, and
maintainable.
const user = {
firstName: 'Robin',
lastName: 'Wieruch',
};
⁹³https://mzl.la/30KbXTC
Fundamentals of React 67
If we need to access multiple properties of an object, using one line of code instead of multiple
lines is often simpler and more elegant. That’s why object destructuring is already widely used in
JavaScript. Before you read the following code, try to transfer this knowledge to the React props in
our Search component yourself.
Let’s see how we can make use of props destructuring here. First, we have to refactor the Search
component’s arrow function from the concise body into block body. And second, we can apply the
destructuring of the props object in the component’s function body:
src/App.js
return (
<div>
<label htmlFor="search">Search: </label>
<input
id="search"
type="text"
value={search}
onChange={onSearch}
/>
</div>
);
};
That’s a basic destructuring of the props object in a React component, so that the object’s
properties can be used conveniently in the component. However, we also had to refactor the Search
component’s arrow function from concise body into block body to access the properties of props
with the object destructuring in the component function’s body. This would happen quite often if
we followed this pattern and it wouldn’t make things easier for us, because we would constantly
have to refactor our components. We can take all this one step further by destructuring the props
object right away in the function signature of our component, omitting the function’s block body
of the component again:
Fundamentals of React 68
src/App.js
React’s props are rarely used in components by themselves; rather, all the information that
is contained in the props object is used. By destructuring the props object right away in the
component’s function signature, we can conveniently access all information without dealing with
its props container. The List and Item components can perform the same props destructuring:
src/App.js
This should be the basic lesson learned from this section, however, we can take this one step further
with a bunch of optional advanced lessons.
Fundamentals of React 69
Nested Destructuring
The incoming item parameter in the Item component has something in common with the previously
discussed props parameter: they are both JavaScript objects. Also, even though the item object has
already been destructured from the props in the Item component’s function signature, it isn’t directly
used in the Item component. The item object only passes its information (object properties) to the
elements.
The current solution is fine as you will see in the ongoing discussion. However, I want to show you
two more variations of it, because there are many things to learn about JavaScript objects in React
here. Let’s get started with nested destructuring and how it works:
Code Playground
const user = {
firstName: 'Robin',
pet: {
name: 'Trixi',
},
};
The nested destructuring helps us to gather all the needed information of the item object in the
function signature for its immediate usage in the component’s elements. Even though it’s not the
most readable option here, note that it can still be useful in other scenarios:
Fundamentals of React 70
src/App.js
const Item = ({
item: {
title,
url,
author,
num_comments,
points,
},
}) => (
<li>
<span>
<a href={url}>{title}</a>
</span>
<span>{author}</span>
<span>{num_comments}</span>
<span>{points}</span>
</li>
);
The nested destructuring helps us to gather all the needed information of the item object in
the function signature for its immediate usage in the component’s elements. However, nested
destructuring introduces lots of clutter through indentations in the function signature. While it’s
here not the most readable option, it can be useful in other scenarios though.
src/App.js
Now, even though the Item component’s function signature is more concise, the clutter ended up in
the List component instead, because every property is passed to the Item component individually.
We can improve this approach using JavaScript’s spread operator⁹⁴:
⁹⁴https://mzl.la/3jetIkn
Fundamentals of React 72
Code Playground
const profile = {
firstName: 'Robin',
lastName: 'Wieruch',
};
const address = {
country: 'Germany',
city: 'Berlin',
};
const user = {
...profile,
gender: 'male',
...address,
};
console.log(user);
// {
// firstName: "Robin",
// lastName: "Wieruch",
// gender: "male"
// country: "Germany,
// city: "Berlin",
// }
JavaScript’s spread operator allows us to literally spread all key/value pairs of an object to another
object. This can also be done in React’s JSX. Instead of passing each property one at a time via
props from List to Item component as before, we can use JavaScript’s spread operator to pass all the
object’s key/value pairs as attribute/value pairs to a JSX element:
src/App.js
This refactoring made the process of passing the information from List to Item component more
concise. Finally, we’ll use JavaScript’s rest destructuring as the icing on the cake. The JavaScript rest
operation happens always as the last part of an object destructuring:
Code Playground
const user = {
id: '1',
firstName: 'Robin',
lastName: 'Wieruch',
country: 'Germany',
city: 'Berlin',
};
console.log(userWithoutAddress);
// {
// firstName: "Robin",
// lastName: "Wieruch"
// }
console.log(id);
// "1"
console.log(city);
// "Berlin"
Even though both have the same syntax (three dots), the rest operator shouldn’t be mistaken with
the spread operator. Whereas the rest operator happens on the left side of an assignment, the spread
Fundamentals of React 74
operator happens on the right side. The rest operator is always used to separate an object from some
of its properties.
Now it can be used in our List component to separate the objectID from the item, because the
objectID is only used as a key and isn’t used in the Item component. Only the remaining (rest) item
gets spread as attribute/value pairs into the Item component (as before):
src/App.js
// Variation 2: Spread and Rest Operators
// Final Step
In this final variation, the rest operator is used to destructure the objectID from the rest of the item
object. Afterward, the item is spread with its key/values pairs into the Item component. While this
final variation is very concise, it comes with advanced JavaScript features that may be unknown to
some.
In this section, we have learned about JavaScript object destructuring which can be commonly used
not only for the props object, but also for other objects like the item object which are nested within
the props. We have also seen how nested destructuring can be used (Variation 1), but also how it
didn’t add any benefits in our case because it just made the component bigger. In the future, you are
more likely to find use cases for nested destructuring which are beneficial.
Last but not least, you have learned about JavaScript’s spread and rest operators, which shouldn’t be
confused with each other, to perform operations on JavaScript objects and to pass the props object
from one component to another component in the most concise way. In the end, I want to point out
the initial version again which we will keep over for the next sections:
Fundamentals of React 75
src/App.js
It may not be the most concise, but it is the easiest to reason about. Variation 1 with its nested
destructuring didn’t add much benefit and variation 2 added too many advanced JavaScript features
(spread operator, rest operator) which are not familiar to everyone. After all, all these variations have
their pros and cons. When refactoring a component, always aim for readability, especially when
working in a team of people, and make sure everyone is using a common React code style.
Rules of thumb:
• Always use object destructuring for props in a function component’s function signature,
because props are rarely used themselves. Exception: When props are only passed through
the component to the next child component (see when to use spread operator).
• Use the spread operator when you want to pass all key/value pairs of an object to a child
component in JSX. For example, often props themselves are not used in a component but
only passed along to the next component. Then it makes sense to just spread the props object
{...props} to the next component.
• Use the rest operator when you only want to split out certain properties from your props object.
• Use nested destructuring only when it improves readability.
Exercises:
• Confirm your source code⁹⁵.
⁹⁵https://bit.ly/30IuiAu
Fundamentals of React 76
⁹⁶https://bit.ly/3G45eUI
⁹⁷https://www.robinwieruch.de/react-pass-props-to-component/
⁹⁸https://mzl.la/30KbXTC
⁹⁹https://mzl.la/3jetIkn
¹⁰⁰https://mzl.la/3GeJbef
¹⁰¹https://forms.gle/WNB4R6yEP1hot3tK8
Fundamentals of React 77
React Side-Effects
A React component’s returned output is defined by its props and state. Side-effects can affect this
output too, because they are used to interact with third-party APIs (e.g. browser’s localStorage API,
remote APIs for data fetching), with HTML element’s for width and height measurements, or with
built-in JavaScript functions such as timers or intervals. These are only a few examples of side-effects
in React components and we will get to apply one of these examples next.
At the moment, whenever you search for a term in our application you will get the result. However,
once you close the browser and open it again, the search term isn’t there anymore. Wouldn’t it be a
great user experience if our Search component could remember the most recent search, so that the
application displays it in the browser whenever it restarts?
Let’s implement this feature by using a side-effect to store the recent search from the browser’s local
storage and retrieve it upon the initial component initialization. First, use the local storage to store
the searchTerm accompanied by an identifier whenever a user types into the HTML input field:
src/App.js
const App = () => {
...
localStorage.setItem('search', event.target.value);
};
...
);
Second, use the stored value, if a value exists, to set the initial state of the searchTerm in React’s
useState Hook. Otherwise, default to our initial state (here: “React”) as before:
src/App.js
const App = () => {
...
...
);
Fundamentals of React 78
Good to know: JavaScript’s logical OR operator¹⁰² returns the truthy operand in this expression and is
short-circuited if localStorage.getItem('search') returns a truthy value. It’s used as a shorthand
for the following implementation for setting default values:
Code Playground
let hasStored;
if (localStorage.getItem('search')) {
hasStored = true;
} else {
hasStored = false;
}
When using the input field and refreshing the browser tab, the browser should remember the latest
search term now. Essentially we synchronized the browser’s local storage with React’s state: While
we initialize the state with the browser’s local storage’s value (or a fallback), we write the new value
when the handler is called to the browser’s storage and the component’s state.
The feature is complete, but there is one flaw that may introduce bugs in the long run: The handler
function should mostly be concerned with updating the state, but it has a side-effect now. The flaw: If
we use the setSearchTerm state updater function somewhere elsewhere in our application, we break
the feature because the local storage doesn’t get updated. Let’s fix this by handling the side-effect
at a centralized place and not in a specific handler. We’ll use React’s useEffect Hook to trigger the
desired side-effect each time the searchTerm changes:
src/App.js
React.useEffect(() => {
localStorage.setItem('search', searchTerm);
}, [searchTerm]);
};
...
);
React’s useEffect Hook takes two arguments: The first argument is a function that runs our side-
effect. In our case, the side-effect stores searchTerm into the browser’s local storage. The second
argument is a dependency array of variables. If one of these variables changes, the function for the
side-effect is called. In our case, the function is called every time the searchTerm changes (e.g. when
a user types into the HTML input field). In addition, it’s also called initially when the component
renders for the first time.
Leaving out the second argument (the dependency array) would make the function for the side-effect
run on every render (initial render and update renders) of the component. If the dependency array
of React’s useEffect is an empty array, the function for the side-effect is only called once when the
component renders for the first time. After all, the hook lets us opt into React’s component lifecycle.
It can be triggered when the component is first mounted, but also if one of its values (state, props,
derived values from state/props) is updated.
In conclusion, using React useEffect Hook instead of managing the side-effect in the (event) handler
has made the application more robust. Whenever and wherever the searchTerm state is updated via
setSearchTerm, the browser’s local storage will always be in sync with it.
Exercises:
• Confirm your source code¹⁰³.
– Confirm the changes¹⁰⁴.
• Read more about React’s useEffect Hook¹⁰⁵.
– Give the first argument’s function a console.log() and experiment with React’s useEffect
Hook’s dependency array. Check the logs for an empty dependency array too.
• Read more about using local storage with React¹⁰⁶.
• Try the following scenario: In your browser, backspace the search term from the input field
until nothing is left there. Internally, it should be set to an empty string now. Next, refresh the
browser and check what it displays. You may be wondering why it does show “React” instead
of “”, because “” should be the recent search. That’s because JavaScript’s logical OR evaluates
“” to false and thus takes “React” as the true value. If you want to prevent this and evaluate “”
as true instead, you may want to exchange JavaScript’s logical OR operator || with JavaScript’s
nullish coalescing operator ??¹⁰⁷.
• Optional: Leave feedback for this section¹⁰⁸.
¹⁰³https://bit.ly/3jj9TbC
¹⁰⁴https://bit.ly/3E12iGK
¹⁰⁵https://www.robinwieruch.de/react-useeffect-hook/
¹⁰⁶https://www.robinwieruch.de/local-storage-react/
¹⁰⁷https://mzl.la/2Z4bsU4
¹⁰⁸https://forms.gle/iCtVZHYt2XRNfAcBA
Fundamentals of React 80
return (
...
);
};
This new custom hook allows us to use it the same way as React’s built-in useState Hook. It returns
a state and a state updater function and accepts an initial state as argument. Under the hood, we
want that this hook synchronizes the state with the browser’s local storage. If you look closely at
the App component in the previous code snippet, you can see that none of the previously introduced
local storage features are there anymore. Instead, we will copy and paste this functionality over to
our new custom hook:
Fundamentals of React 81
src/App.js
React.useEffect(() => {
localStorage.setItem('search', searchTerm);
}, [searchTerm]);
};
So far, this custom hook is just a function around the useState and useEffect hooks which we’ve
previously used in the App component. What’s missing is providing an initial state and returning
the values that are needed in our App component as an array:
src/App.js
React.useEffect(() => {
localStorage.setItem('search', searchTerm);
}, [searchTerm]);
We are following two conventions of React’s built-in hooks here. First, the naming convention which
puts the “use” prefix in front of every hook name. And second, the returned values are returned as
an array. Another goal of a custom hook should be reusability. All of this custom hook’s internals are
about a certain search domain, however, to make the custom hook reusable and therefore generic,
we have to adjust the internal names:
Fundamentals of React 82
src/App.js
const useStorageState = (initialState) => {
const [value, setValue] = React.useState(
localStorage.getItem('value') || initialState
);
React.useEffect(() => {
localStorage.setItem('value', value);
}, [value]);
Now we handle an abstracted “value” within the custom hook. Using it in the App component,
we can name the returned current state and state updater function anything domain-related (e.g.
searchTerm and setSearchTerm) with array destructuring.
There is still one problem with this custom hook. Using the custom hook more than once in a React
application leads to an overwrite of the “value”-allocated item in the local storage, because it uses
the same key in the local storage. To fix this, pass in a flexible key. Since the key comes from outside,
the custom hook assumes that it could change, so it needs to be included in the dependency array
of the useEffect hook as well. Without it, the side-effect may run with an outdated key (also called
stale) if the key changed between renders:
src/App.js
const useStorageState = (key, initialState) => {
const [value, setValue] = React.useState(
localStorage.getItem(key) || initialState
);
React.useEffect(() => {
localStorage.setItem(key, value);
}, [value, key]);
);
...
};
You’ve just created your first custom hook. If you’re not comfortable with custom hooks, you can
revert the changes and use the useState and useEffect hook as before in the App component.
However, knowing about custom hooks gives you lots of new options. A custom hook can
encapsulate non-trivial implementation details that should be kept away from a component, can
be used in more than one React component, can be a composition of other hooks, and can even
be open-sourced as an external library. Using your favorite search engine, you’ll notice there are
hundreds of React hooks that could be used in your application without worry over implementation
details.
Exercises:
• Confirm your source code¹⁰⁹.
– Confirm the changes¹¹⁰.
• Read more about React Hooks¹¹¹ and custom React Hooks¹¹² to get a good understanding of
them, because they are the bread and butter in React function components.
• Read more about custom React Hooks¹¹³.
• Optional: Leave feedback for this section¹¹⁴.
¹⁰⁹https://bit.ly/30Koneb
¹¹⁰https://bit.ly/2ZbkAGm
¹¹¹https://www.robinwieruch.de/react-hooks/
¹¹²/react-custom-hook/
¹¹³https://www.robinwieruch.de/react-custom-hooks/
¹¹⁴https://forms.gle/5seN1Rv3ZwXmWmDR9
Fundamentals of React 84
React Fragments
You may have noticed that all of our React components return JSX with one top-level HTML element.
When we introduced the Search component a while ago, we had to add a <div> tag (read: container
element), because otherwise the label and input elements couldn’t be returned side-by-side without
a wrapping top-level element:
src/App.js
However, there are ways to render multiple top-level elements side-by-side. A rarely used approach
returns all sibling elements as an array. Since this resembles a list of elements, we would have to
give each list item a mandatory key attribute:
src/App.js
Fortunately there exists another way of returning siblings elements side-by-side without a top-level
element, because the last approach with the array doesn’t turn out very readable and becomes
verbose with the additional key attribute. Another solution is to use a React fragment:
Fundamentals of React 85
src/App.js
const Search = ({ search, onSearch }) => (
<React.Fragment>
<label htmlFor="search">Search: </label>
<input
id="search"
type="text"
value={search}
onChange={onSearch}
/>
</React.Fragment>
);
A fragment wraps sibling elements into a single top-level element without adding them to the
rendered output. See for yourself by inspecting the elements in your browser’s development tools
after using a fragment in your React component. A more popular alternative these days is using
fragments in their shorthand version:
src/App.js
const Search = ({ search, onSearch }) => (
<>
<label htmlFor="search">Search: </label>
<input
id="search"
type="text"
value={search}
onChange={onSearch}
/>
</>
);
Both elements in the Search component, input field and label, should be still visible in your browser
now. After all, whenever you don’t want to introduce an intermediary element that’s only there to
satisfy React, you can use fragments as helper “elements”.
Exercises:
• Confirm your source code¹¹⁵.
– Confirm the changes¹¹⁶.
• Optional: Leave feedback for this section¹¹⁷.
¹¹⁵https://bit.ly/3piEnhG
¹¹⁶https://bit.ly/3n9Rmjd
¹¹⁷https://forms.gle/kNpEySPZzckNe6f96
Fundamentals of React 86
return (
<div>
<h1>My Hacker Stories</h1>
<InputWithLabel
id="search"
label="Search"
value={searchTerm}
onInputChange={handleSearch}
/>
...
</div>
);
};
type="text"
value={value}
onChange={onInputChange}
/>
</>
);
It’s fully reusable, but only when using an input with a text. If we would want to support numbers
(number) or phone numbers (tel) too, the type attribute of the input field needs to be accessible from
the outside too:
src/App.js
const InputWithLabel = ({
id,
label,
value,
type = 'text',
onInputChange,
}) => (
<>
<label htmlFor={id}>{label}</label>
<input
id={id}
type={type}
value={value}
onChange={onInputChange}
/>
</>
);
Because we don’t pass a type prop from the App component to the InputWithLabel component,
the default parameter¹¹⁸ from the function signature takes over for the type. Thus, every time the
InputWithLabel component is used without a type prop, the default type will be "text".
With just a few changes we turned a specialized Search component into a more reusable InputWith-
Label component. We generalized the naming of the internal implementation details and gave the
new component a larger API surface to provide all the necessary information from the outside. We
aren’t using the component elsewhere yet, but we increased its ability to handle the task if we do.
It’s always a trade-off between generalization and specialization of components. In this case,
we turned a highly specialized component into a generalized component. While a generalized
¹¹⁸https://mzl.la/3aUefkN
Fundamentals of React 88
component has a better chance of getting reused in the application, a specialized component would
implement business logic for one specific use case and therefore isn’t reusable at all.
Exercises:
• Confirm your source code¹¹⁹.
– Confirm the changes¹²⁰.
• Read more about Reusable React Components¹²¹ and build some of these components yourself:
– Dropdown in React¹²²
– Checkbox in React¹²³
– Button in React¹²⁴
– Radio Button in React¹²⁵
• Before we used the text “Search:” with a “:”. How would you deal with it now? Would you pass
it with label="Search:" as prop to the InputWithLabel component or hardcode it after the
<label htmlFor={id}>{label}:</label> usage in the InputWithLabel component? We will
see how to cope with this later.
• Optional: Leave feedback for this section¹²⁶.
¹¹⁹https://bit.ly/3B0roTU
¹²⁰https://bit.ly/3C2DzAY
¹²¹https://www.robinwieruch.de/react-reusable-components/
¹²²https://www.robinwieruch.de/react-dropdown/
¹²³https://www.robinwieruch.de/react-checkbox/
¹²⁴https://www.robinwieruch.de/react-button/
¹²⁵https://www.robinwieruch.de/react-radio-button/
¹²⁶https://forms.gle/76C3LvW3kHHwdhgq5
Fundamentals of React 89
return (
<div>
<h1>My Hacker Stories</h1>
<InputWithLabel
id="search"
value={searchTerm}
onInputChange={handleSearch}
>
Search:
</InputWithLabel>
...
</div>
);
};
Component composition is one of React’s more powerful features. Essentially we’ll discover how to
use a React element in the same fashion as an HTML element by leveraging its opening and closing
tag.
In the previous example, instead of using the label prop from before, we inserted the text “Search:”
between the component’s element’s tags. In the InputWithLabel component, you have access to this
information via React’s children prop now. Instead of using the label prop, use the children prop
to render everything that has been rendered in between the <InputWithLabel> opening and closing
tag:
Fundamentals of React 90
src/App.js
const InputWithLabel = ({
id,
value,
type = 'text',
onInputChange,
children,
}) => (
<>
<label htmlFor={id}>{children}</label>
<input
id={id}
type={type}
value={value}
onChange={onInputChange}
/>
</>
);
Now the React component’s elements behave similarly to native HTML. Everything that’s passed
between a component’s elements can be accessed as children in the component and be rendered
somewhere. Sometimes when using a React component, you want to have more freedom from the
outside regarding what to render on the inside of a component:
src/App.js
return (
<div>
<h1>My Hacker Stories</h1>
<InputWithLabel
id="search"
value={searchTerm}
onInputChange={handleSearch}
>
<strong>Search:</strong>
</InputWithLabel>
...
Fundamentals of React 91
</div>
);
};
With the React children prop, we can compose React components into each other. We’ve used it with
a string and with a string wrapped in an HTML <strong> element, but it doesn’t end here. You can
pass React elements via React children as well – which you should definitely explore more as an
exercise.
Exercises:
• Confirm your source code¹²⁷.
– Confirm the changes¹²⁸.
• Read more about Component Composition in React¹²⁹.
• Optional: Leave feedback for this section¹³⁰.
¹²⁷https://bit.ly/3lUZqVA
¹²⁸https://bit.ly/3BU66J3
¹²⁹https://www.robinwieruch.de/react-component-composition/
¹³⁰https://forms.gle/L2GgfHVjAAwbqudq8
Fundamentals of React 92
Imperative React
React is inherently declarative. When you implement JSX, you tell React what elements you want
to see, not how to create these elements. When you implement a hook for state, you tell React what
you want to manage as a stateful value and not how to manage it. And when you implement an
event handler, you do not have to assign a listener imperatively:
// declarative React
const App = () => {
const handleClick = () => {
// do something
};
return (
<button
type="button"
onClick={handleClick}
>
Click
</button>
);
};
However, there are cases when we will not want everything to be declarative. For example,
sometimes you want to have imperative access to rendered elements, most often as a side-effect,
in cases such as these:
Because imperative programming in React is often verbose and counterintuitive, we’ll walk through
only a small example for setting the focus of an input field imperatively. For the declarative way,
simply set the input field’s autofocus attribute:
¹³¹https://d3js.org
Fundamentals of React 93
src/App.js
This works, but only if one of the reusable components is rendered. For example, if the App
component renders two InputWithLabel components, only the last rendered component receives
the autoFocus on its render. However, since we have a reusable React component here, we can
pass a dedicated prop which lets the developer decide whether the input field should have an active
autoFocus:
src/App.js
return (
<div>
<h1>My Hacker Stories</h1>
<InputWithLabel
id="search"
value={searchTerm}
isFocused
onInputChange={handleSearch}
>
<strong>Search:</strong>
</InputWithLabel>
...
Fundamentals of React 94
</div>
);
};
Again, using just isFocused as an attribute is equivalent to isFocused={true}. Within the compo-
nent, use the new prop for the input field’s autoFocus attribute:
src/App.js
const InputWithLabel = ({
id,
value,
type = 'text',
onInputChange,
isFocused,
children,
}) => (
<>
<label htmlFor={id}>{children}</label>
<input
id={id}
type={type}
value={value}
autoFocus={isFocused}
onChange={onInputChange}
/>
</>
);
The feature works, yet it’s still a declarative implementation. We are telling React what to do and
not how to do it. Even though it’s possible to do it with the declarative approach, let’s refactor this
scenario to an imperative approach. We want to execute the focus() method programmatically on
the input field’s element via the DOM API once it has been rendered:
Fundamentals of React 95
src/App.js
const InputWithLabel = ({
id,
value,
type = 'text',
onInputChange,
isFocused,
children,
}) => {
// A
const inputRef = React.useRef();
// C
React.useEffect(() => {
if (isFocused && inputRef.current) {
// D
inputRef.current.focus();
}
}, [isFocused]);
return (
<>
<label htmlFor={id}>{children}</label>
{/* B */}
<input
ref={inputRef}
id={id}
type={type}
value={value}
onChange={onInputChange}
/>
</>
);
};
All the essential steps are marked with comments that are explained step by step:
• (A) First, create a ref with React’s useRef Hook. This ref object is a persistent value which
stays intact over the lifetime of a React component. It comes with a property called current,
which, in contrast to the ref object, can be changed.
Fundamentals of React 96
• (B) Second, the ref is passed to the element’s JSX-reserved ref attribute and thus element
instance gets assigned to the changeable current property.
• (C) Third, opt into React’s lifecycle with React’s useEffect Hook, performing the focus on the
element when the component renders (or its dependencies change).
• (D) And fourth, since the ref is passed to the element’s ref attribute, its current property gives
access to the element. Execute its focus programmatically as a side-effect, but only if isFocused
is set and the current property is existent.
Essentially that’s the whole example of how to move from declarative to imperative programming
in React. In this case, it’s possible to use either the declarative or imperative approach as you
experienced first hand. However, it’s not always possible to use the declarative approach, so the
imperative approach can be performed whenever it’s necessary.
Exercises:
• Confirm your source code¹³².
– Confirm the changes¹³³.
• Read more about refs in React¹³⁴ and optionally check out the following tutorials which are
using refs:
– Create a Slider component with a ref¹³⁵
– Create an image from a React component with a ref¹³⁶
– Create a custom hook with a ref¹³⁷
• Read more about why frameworks matter¹³⁸.
• Optional: Leave feedback for this section¹³⁹.
¹³²https://bit.ly/3B0qc2S
¹³³https://bit.ly/3aT3bEq
¹³⁴https://www.robinwieruch.de/react-ref/
¹³⁵https://www.robinwieruch.de/react-slider/
¹³⁶https://www.robinwieruch.de/react-component-to-image/
¹³⁷https://www.robinwieruch.de/react-custom-hook-check-if-overflow/
¹³⁸https://www.robinwieruch.de/why-frameworks-matter/
¹³⁹https://forms.gle/nABoW2tKAPd1yVkv7
Fundamentals of React 97
• The list of items needs to become a stateful value in order to manipulate (e.g. removing an
item) it later.
• Every list item renders a button with a click handler. When clicking the button, the item gets
removed from the list by manipulating the state.
• Since the stateful list resides in the App component, one needs to use callback handlers to
enable the Item component to communicate up to the App component for removing an item
by its identifier.
Now we want to check out how to implement this feature step by step. At the moment, the list of
items (here: stories) that we have in our App component is an unstateful variable. We can filter the
rendered list with the search feature, but the list itself stays intact. The filtered list is just a derived
state through a third-party (here: searchTerm), but we do not manipulate the actual list yet. To gain
control over the list, make it stateful by using it as initial state in React’s useState Hook. The returned
values from the array are the current state (stories) and the state updater function (setStories):
src/App.js
const initialStories = [
{
title: 'React',
...
},
{
title: 'Redux',
...
},
];
...
Fundamentals of React 98
...
};
The application behaves the same because the stories, now returned as a stateful list from React’s
useState Hook, are still filtered into searchedStories and displayed in the List component. Just the
origin where the stories are coming from has changed. Next, we will write an event handler which
removes an item from the list:
src/App.js
setStories(newStories);
};
...
return (
<div>
<h1>My Hacker Stories</h1>
...
<hr />
The callback handler in the App component – which will be used in the List/Item components
eventually – receives the item as an argument which should be removed from the list. Based on
this information, the function filters the current stories by removing all items that don’t meet its
condition. The returned stories – where the desired item (story) has been removed – are then set as
a new state and passed to the List component. Since a new state is set, the App component and all
components below (e.g. List/Item components) will render again and thus display the new state of
stories.
However, what’s missing is how the List/Item components are using this new functionality which
modifies the state in the App component. The List component itself does not use this new callback
handler, but only passes it on to the Item component:
src/App.js
Finally, the Item component uses the incoming callback handler as a function in a new handler. In
this handler, we will pass the specific item to it. Moreover, an additional button element is needed
to trigger the actual event:
src/App.js
return (
<li>
<span>
<a href={item.url}>{item.title}</a>
</span>
<span>{item.author}</span>
<span>{item.num_comments}</span>
<span>{item.points}</span>
Fundamentals of React 100
<span>
<button type="button" onClick={handleRemoveItem}>
Dismiss
</button>
</span>
</li>
);
};
So far in this section, we have made the list of stories stateful with React’s useState Hook, passed
the still searched stories down as props to the List component, and implemented a callback handler
(handleRemoveStory) and handler (handleRemoveItem) to be used in their respective components to
remove a story by clicking on a button. In order to implement this feature, we applied many lessons
learned from before: state, props, handlers, and callback handlers. The feature works and you may
have arrived at the same or a similar solution yourself.
Let’s enter the topic of inline handlers: You may have noticed that we had to introduce an additional
handleRemoveItem handler in the Item component which is in charge to execute the incoming
onRemoveItem callback handler. We had to introduce this extra event handler to pick up the item
as argument for the callback handler.
If you want to make this more elegant though, you can use an inline handler which allows you
to execute the callback handler function in the Item component right in the JSX. There are two
solutions using the incoming onRemoveItem function in the Item component as an inline handler.
First, using JavaScript’s bind method:
src/App.js
Using JavaScript’s bind method¹⁴⁰ on a function allows us to bind arguments directly to that function
that should be used when executing it. The bind method returns a new function with the bound
argument attached. In contrast, the second and more popular solution is to use an inline arrow
function, which allows us to sneak in arguments like the item:
src/App.js
While using an inline handler is more concise than using a normal event handler, it can also be more
difficult to debug because JavaScript logic may be hidden in JSX. It becomes even more verbose if
the inline arrow function encapsulates more than one line of implementation logic by using a block
body instead of a concise body:
Code Playground
onRemoveItem(item);
}}
¹⁴⁰https://mzl.la/3ncEkBu
Fundamentals of React 102
>
Dismiss
</button>
</span>
</li>
);
As a rule of thumb: It’s okay to use inline handlers if they do not obscure critical implementation
details. If inline handlers need to use a block body, because there are more than one line of code
executed, it’s about time to extract them as normal event handlers. After all, in this case all handler
versions are readable and therefore acceptable.
Exercises:
• Confirm your source code¹⁴¹.
– Confirm the changes¹⁴².
• Read more about how to add¹⁴³, update¹⁴⁴, remove¹⁴⁵ items in a list.
• Read more about computed properties in React¹⁴⁶.
• Review handlers, callback handlers, and inline handlers.
• Optional: Leave feedback for this section¹⁴⁷.
¹⁴¹https://bit.ly/3vrGWzb
¹⁴²https://bit.ly/3jj37CR
¹⁴³https://www.robinwieruch.de/react-add-item-to-list
¹⁴⁴https://www.robinwieruch.de/react-update-item-in-list/
¹⁴⁵https://www.robinwieruch.de/react-remove-item-from-list
¹⁴⁶https://www.robinwieruch.de/react-computed-properties/
¹⁴⁷https://forms.gle/19NvNYMk2RUKTDyZ6
Fundamentals of React 103
In the App component, instead of using the initialStories, use an empty array for the initial state.
We want to start off with an empty list of stories and simulate fetching these stories asynchronously.
In a new useEffect hook, call the function and resolve the returned promise as a side-effect. Due
to the empty dependency array, the side-effect only runs once the component renders for the first
time:
src/App.js
React.useEffect(() => {
getAsyncStories().then(result => {
setStories(result.data.stories);
});
}, []);
...
};
Fundamentals of React 104
Even though the data should arrive asynchronously when we start the application, it appears to
arrive synchronously, because it’s rendered immediately. Let’s change this by giving it a bit of a
realistic delay, because every network request to a remote API would come with a delay. First,
remove the shorthand version for the promise:
src/App.js
const getAsyncStories = () =>
new Promise((resolve) =>
resolve({ data: { stories: initialStories } })
);
Once you start the application again, you should see a delayed rendering of the list. The initial state
for the stories is an empty array and therefore nothing gets rendered in the List component. After
the App component is rendered, the side-effect hook runs once to fetch the asynchronous data. After
resolving the promise and setting the data in the component’s state, the component renders again
and displays the list of asynchronously loaded stories.
This section was only the first stepping stone to asynchronous data in React. Instead of having the
data there from the beginning, we resolved the data asynchronously from a promise. However, we
only moved our stories from being synchronous to asynchronous data. It’s still sample data though
and we will learn how to fetch real data eventually.
Exercises:
• Confirm your source code¹⁴⁸.
– Confirm the changes¹⁴⁹.
• Optional: Read more about JavaScript Promises¹⁵⁰.
• Read more about faking a remote API with JavaScript¹⁵¹.
– Read more about using mock data in React¹⁵².
• Optional: Leave feedback for this section¹⁵³.
¹⁴⁸https://bit.ly/3vu6kEb
¹⁴⁹https://bit.ly/3B0p7rQ
¹⁵⁰https://mzl.la/3aTGuQz
¹⁵¹https://www.robinwieruch.de/javascript-fake-api/
¹⁵²https://www.robinwieruch.de/react-mock-data/
¹⁵³https://forms.gle/sfQcc477xmgGRLyB7
Fundamentals of React 105
• In order to show a loading indicator, one would need to introduce a new stateful value. A
boolean called isLoading may be the best solution.
• When the side-effect which loads the data kicks in, set the stateful boolean to true. Once the
data loaded, set the stateful boolean to false again.
• In JSX, show a “Loading …” text conditionally when the isLoading boolean is set to true.
A conditional rendering in React always happens if we have to render different JSX based on
information (e.g. state, props). Dealing with asynchronous data is a good use case for making use
of conditional rendering. For example, when the application initializes for the first time, there is no
data to start with. Next, we are loading data and eventually, we have the data at our hands to display
it. Sometimes the data fetching fails and we receive an error instead. So there are lots of things to
cover for us as developers.
Fortunately, a few of these cases are already taken care of. For instance, because the initial state is
an empty list [] rather than null, we don’t have to worry that this breaks the application when we
filter and map over this list. However, a few mentioned things are still missing. For example, what
about a loading state which will show the user a loading indicator as feedback about the pending
data request? We could introduce this as new stateful value and set the state accordingly when data
gets fetched:
src/App.js
React.useEffect(() => {
setIsLoading(true);
getAsyncStories().then((result) => {
Fundamentals of React 106
setStories(result.data.stories);
setIsLoading(false);
});
}, []);
...
};
The boolean should be toggled properly now. What’s missing is showing the user the loading
indicator. A straightforward approach would be using an early return in the App component:
src/App.js
if (isLoading) {
return <p>Loading ...</p>;
}
return (
<div>
...
</div>
);
};
However, this way only the loading indicator would render and nothing else. Instead, we want to
inline the loading indicator within the JSX to either show the loading indicator or the List component.
Using an if-else statement inlined in JSX is not encouraged though due to JSX’s limitations here.
(You can try it as exercise though.) However, you can use a ternary operator¹⁵⁴ instead and produce
a conditional rendering in JSX this way:
¹⁵⁴https://mzl.la/3vAPKCL
Fundamentals of React 107
src/App.js
return (
<div>
...
<hr />
{isLoading ? (
<p>Loading ...</p>
) : (
<List
list={searchedStories}
onRemoveItem={handleRemoveStory}
/>
)}
</div>
);
};
That’s already it. You are rendering conditionally a loading indicator or the List component based
on a stateful boolean. Let’s move on by implementing error handling for the asynchronous data too.
An error doesn’t happen in our simulated environment, but there could be errors if we start fetching
data from a remote API. Therefore, introduce another state for error handling and handle it in the
promise’s catch() block when resolving the promise:
src/App.js
React.useEffect(() => {
setIsLoading(true);
getAsyncStories()
.then((result) => {
setStories(result.data.stories);
Fundamentals of React 108
setIsLoading(false);
})
.catch(() => setIsError(true));
}, []);
...
};
Next, give the user feedback in case something goes wrong with another conditional rendering. This
time, it’s either rendering something or nothing. So instead of having a ternary operator where one
side returns null, use the logical && operator as shorthand:
src/App.js
return (
<div>
...
<hr />
{isLoading ? (
<p>Loading ...</p>
) : (
...
)}
</div>
);
};
In JavaScript, a true && 'Hello World' always evaluates to ‘Hello World’. A false && 'Hello World'
always evaluates to false. In React, we can use this behaviour to our advantage. If the condition is
true, the expression after the logical && operator will be the output. If the condition is false, React
ignores it and skips the expression. Using expession && JSX is more concise than using expression
? JSX : null.
Conditional rendering is not just for asynchronous data though. The simplest example of conditional
rendering is a boolean state that’s toggled with a button. If the boolean flag is true, render something,
if it is false, don’t render anything. Knowing about this feature in React can be quite powerful,
Fundamentals of React 109
because it gives you the ability to conditionally render JSX. It’s yet another tool in React to make
your UI more dynamic. And as we’ve discovered, it’s often necessary for more complex control
flows like asynchronous data.
Exercises:
• Confirm your source code¹⁵⁵.
– Confirm the changes¹⁵⁶.
• Read more about conditional rendering in React¹⁵⁷.
• Question: Why didn’t we need a conditional rendering for the empty stories before they get
fetched from the fake API?
– Answer: The stories are mapped as list in the List component by using the map() method.
When mapping over a list, the map() method returns for every item a modified version
(in our case JSX). If there are no items in the list, the map() method will return nothing.
Therefore we do not need a conditional rendering here.
• Question: What would happen if the initial state of stories would be set to null instead of
[]?
– Answer: Then we would need a conditional rendering in the List component, because
calling map() on null would throw an exception.
• Optional: Leave feedback for this section¹⁵⁸.
¹⁵⁵https://bit.ly/2ZfIJLM
¹⁵⁶https://bit.ly/3AYmneE
¹⁵⁷https://www.robinwieruch.de/conditional-rendering-react/
¹⁵⁸https://forms.gle/kHLAXtMaKsTFtWjY9
Fundamentals of React 110
A reducer action is always associated with a type and as a best practice with a payload. If the type
matches a condition in the reducer, return a new state based on incoming state and action. If it isn’t
covered by the reducer, throw an error to remind yourself that the implementation isn’t covered.
The storiesReducer function covers one type and then returns the payload of the incoming action
without using the current state to compute the new state. The new state is therefore simply the
payload.
In the App component, exchange useState for useReducer for managing the stories. The new hook
receives a reducer function and an initial state as arguments and returns an array with two items.
The first item is the current state and the second item is the state updater function (also called
dispatch function):
¹⁵⁹https://www.robinwieruch.de/javascript-reducer/
Fundamentals of React 111
src/App.js
...
};
The new dispatch function can be used instead of the setStories function, which was previously
returned from useState. Instead of setting the state explicitly with the state updater function from
useState, the useReducer state updater function sets the state implicitly by dispatching an action
for the reducer. The action comes with a type and an optional payload:
src/App.js
React.useEffect(() => {
setIsLoading(true);
getAsyncStories()
.then((result) => {
dispatchStories({
type: 'SET_STORIES',
payload: result.data.stories,
});
setIsLoading(false);
})
.catch(() => setIsError(true));
}, []);
dispatchStories({
type: 'SET_STORIES',
Fundamentals of React 112
payload: newStories,
});
};
...
};
The application appears the same in the browser, though a reducer and React’s useReducer hook are
managing the state for the stories now. Let’s bring the concept of a reducer to a minimal version
by handling more than one state transition. If there is only one state transition, a reducer wouldn’t
make sense.
So far, the handleRemoveStory handler computes the new stories. It’s valid to move this logic into
the reducer function and manage the reducer with an action, which is another case for moving from
imperative to declarative programming. Instead of doing it ourselves by saying how it should be
done, we are telling the reducer what to do. Everything else is hidden in the reducer:
src/App.js
...
};
Now the reducer function has to cover this new case in a new conditional state transition. If the
condition for removing a story is met, the reducer has all the implementation details needed to
remove the story. The action gives all the necessary information (here an item’s identifier‘) to remove
the story from the current state and return a new list of filtered stories as state:
Fundamentals of React 113
src/App.js
All these if-else statements will eventually clutter when adding more state transitions into one
reducer function. Refactoring it to a switch statement for all the state transitions makes it more
readable and is seen as a best practice in the React community:
src/App.js
What we’ve covered is a minimal version of a reducer in JavaScript and its usage in React with the
help of React’s useReducer Hook. The reducer covers two state transitions, shows how to compute
the current state and action into a new state, and uses some business logic (removal of a story)
for a state transition. Now we can set a list of stories as state for the asynchronously arriving
data and remove a story from the list of stories with just one state managing reducer and its
associated useReducer hook. To fully grasp the concept of reducers in JavaScript and the usage
of React’s useReducer Hook, visit the linked resources in the exercises. We will continue expanding
our implementation of a reducer in the next section.
Fundamentals of React 114
Exercises:
• Confirm your source code¹⁶⁰.
– Confirm the changes¹⁶¹.
• Read more about reducers and useReducer in React¹⁶².
• Extract the action types 'SET_STORIES' and 'REMOVE_STORY' as variables and reuse them in the
reducer and the dispatch functions. This way, you will avoid introducing typos in your action
types.
• Optional: Leave feedback for this section¹⁶³.
¹⁶⁰https://bit.ly/3nbb5Pp
¹⁶¹https://bit.ly/3lZrV4y
¹⁶²https://www.robinwieruch.de/react-usereducer-hook/
¹⁶³https://forms.gle/tNqqVynwQV9Ym9u68
Fundamentals of React 115
The impossible state happens when an error occurs for the asynchronous data. The state for the error
is set, but the state for the loading indicator isn’t revoked. In the UI, this would lead to an infinite
loading indicator and an error message, though it may be better to show the error message only and
hide the loading indicator. Impossible states are not easy to spot, which makes them infamous for
causing bugs in the UI. You could go on and try yourself to fix this bug.
Fortunately, we can improve our chances of not dealing with such bugs by moving states that belong
together from multiple useState (and useReducer) hooks into a single useReducer hook. Take the
following hooks:
src/App.js
const App = () => {
...
...
};
And merge them into one useReducer hook for a unified state management which encompasses a
more complex state object and eventually more complex state transitions:
Fundamentals of React 116
src/App.js
...
};
Since we cannot use the state updater functions from React’s useState Hooks anymore, everything
related to asynchronous data fetching must use the new dispatch function for the state transitions.
The most straightforward approach is exchanging the state updater function with the dispatch
function. Then the dispatch function receives a distinct type and a payload. The latter resembles
the same payload that we would have used to update the state with a state updater function:
src/App.js
React.useEffect(() => {
dispatchStories({ type: 'STORIES_FETCH_INIT' });
getAsyncStories()
.then((result) => {
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: result.data.stories,
});
})
.catch(() =>
dispatchStories({ type: 'STORIES_FETCH_FAILURE' })
);
}, []);
Fundamentals of React 117
...
};
We changed two things for the reducer function. First, we introduced new types when we called the
dispatch function from the outside. Therefore we need to add new cases for state transitions. And
second, we changed ti state structure from an array to a complex object. Therefore we need to take
the new complex object into account as incoming state and returned state:
src/App.js
For every state transition, we return a new state object which contains all the key/value pairs from
the current state object (via JavaScript’s spread operator) and the new overwriting properties. For
example, STORIES_FETCH_FAILURE sets the isLoading boolean to false and sets the isError boolean
to true, while keeping all the the other state intact (e.g. data alias stories). That’s how we get
around the bug introduced earlier as impossible state since an error should set the loading boolean
to false.
Observe how the REMOVE_STORY action changed as well. It operates on the state.data, and no longer
just on the plain state. The state is a complex object with data, isLoading, and error states rather
than just a list of stories. This has to be solved in the remaining code by addressing the state as object
and not as array anymore:
src/App.js
const App = () => {
...
...
return (
<div>
...
{stories.isLoading ? (
<p>Loading ...</p>
) : (
<List
list={searchedStories}
onRemoveItem={handleRemoveStory}
/>
)}
</div>
);
};
Fundamentals of React 119
Try to use the erroneous data fetching function again and check whether everything works as
expected now:
src/App.js
We moved from unreliable state transitions with multiple useState hooks to predictable state
transitions with React’s useReducer Hook. The state object managed by the reducer encapsulates
everything related to the fetching of stories including loading and error states, but also implementa-
tion details like removing a story from the stories. We didn’t get fully rid of impossible states, because
it’s still possible to leave out a crucial boolean flag like before, but we moved one step closer towards
more predictable state management.
Exercises:
• Confirm your source code¹⁶⁴.
– Confirm the changes¹⁶⁵.
• Read more about when to use useState or useReducer in React¹⁶⁶.
• Read more about deriving state from props in React¹⁶⁷.
• Optional: Leave feedback for this section¹⁶⁸.
¹⁶⁴https://bit.ly/3G6AphY
¹⁶⁵https://bit.ly/3jepMA7
¹⁶⁶https://www.robinwieruch.de/react-usereducer-vs-usestate/
¹⁶⁷https://www.robinwieruch.de/react-derive-state-props/
¹⁶⁸https://forms.gle/XWTJS65iu6WkiZMCA
Fundamentals of React 120
We start with a great foundation for fetching asynchronous data, because everything is already in
place. The only thing that keeps us away from the solution is using sample data instead of real
world data. Therefore, the next code snippet shows everything you need to change to connect to a
remote API. Instead of using the initialStories array and getAsyncStories function, which can
be removed now, we will fetch the data directly from the API:
src/App.js
// A
const API_ENDPOINT = 'https://hn.algolia.com/api/v1/search?query=';
React.useEffect(() => {
dispatchStories({ type: 'STORIES_FETCH_INIT' });
fetch(`${API_ENDPOINT}react`) // B
.then((response) => response.json()) // C
¹⁶⁹https://hn.algolia.com/api
¹⁷⁰https://mzl.la/2Z1kyjU
Fundamentals of React 121
.then((result) => {
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: result.hits, // D
});
})
.catch(() =>
dispatchStories({ type: 'STORIES_FETCH_FAILURE' })
);
}, []);
...
};
First, the API_ENDPOINT (A) is used to fetch popular tech stories for a certain query (a search term).
In this case, we fetch stories about React (B). Second, the native browser’s fetch API¹⁷¹ is used to
make this request (B). For the fetch API, the response needs to be translated into JSON (C). Finally,
the returned result has a different data structure (D), which we send as payload to our component’s
state reducer.
In the previous code example, we used JavaScript’s Template Literals¹⁷² for a string interpolation.
When this feature wasn’t available in JavaScript, we’d have used the + operator on strings instead:
Code Playground
const greeting = 'Hello';
// + operator
const welcome = greeting + ' React';
console.log(welcome);
// Hello React
// template literals
const anotherWelcome = `${greeting} React`;
console.log(anotherWelcome);
// Hello React
Check your browser to see stories related to the initial query fetched from the Hacker News API.
Since we used the same data structure for a story for the sample stories, we didn’t need to change
anything in the Item component, and it’s still possible to filter the stories after fetching them with
the search feature, because they still have a title property. We will change this behavior in one of
the next sections though.
¹⁷¹https://mzl.la/2Z1kyjU
¹⁷²https://mzl.la/3jlcVfn
Fundamentals of React 122
Exercises:
• Confirm your source code¹⁷³.
– Confirm the changes¹⁷⁴.
• Read through Hacker News¹⁷⁵ and its API¹⁷⁶.
• Optional: Read more about JavaScript’s Template Literals¹⁷⁷.
• Optional: Leave feedback for this section¹⁷⁸.
¹⁷³https://bit.ly/3DYWxZS
¹⁷⁴https://bit.ly/3jks1Sj
¹⁷⁵https://news.ycombinator.com
¹⁷⁶https://hn.algolia.com/api
¹⁷⁷https://mzl.la/3jlcVfn
¹⁷⁸https://forms.gle/hoJxjjpoZQGCS7Vp9
Fundamentals of React 123
• The derived value searchedStories can be removed, because we expect the data to come
filtered from the API.
• For the data fetching, the hardcoded 'react' needs to get replaced by the searchTerm.
• Handle the edge case when searchTerm is an empty string.
There are not many steps involved to migrate the application from a client-side to a server-side
search. First, remove searchedStories because we will receive the stories filtered by search term
from the API. Pass only the regular stories to the List component:
src/App.js
const App = () => {
...
return (
<div>
...
{stories.isLoading ? (
<p>Loading ...</p>
) : (
<List list={stories.data} onRemoveItem={handleRemoveStory} />
)}
</div>
);
};
Fundamentals of React 124
And second, instead of using the hardcoded search term (here: 'react'), use the actual searchTerm
from the component’s state. Afterward, every time a user searches for something via the input field,
the searchTerm will be used to request these kind of stories from the remote API. In addition, you
need to deal with the edge case if searchTerm is an empty string, which means preventing a request
from being fired:
src/App.js
React.useEffect(() => {
if (searchTerm === '') return;
fetch(`${API_ENDPOINT}${searchTerm}`)
.then((response) => response.json())
.then((result) => {
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: result.hits,
});
})
.catch(() =>
dispatchStories({ type: 'STORIES_FETCH_FAILURE' })
);
}, []);
...
};
There is one crucial piece missing now. While the initial data fetching respects the searchTerm (here:
'React' which is set as initial state), the searchTerm is not respected when it is changed via a user
typing into the input field. If you inspect the dependency array of our useEffect hook, you will
see that it’s empty. This means the side-effect only renders for the initial rendering of the App
component. If we would want to run the side-effect also when the searchTerm changes, we would
have to include it in the dependency array:
Fundamentals of React 125
src/App.js
React.useEffect(() => {
// if `searchTerm` is not present
// e.g. null, empty string, undefined
// do nothing
// more generalized condition than searchTerm === ''
if (!searchTerm) return;
fetch(`${API_ENDPOINT}${searchTerm}`)
.then((response) => response.json())
.then((result) => {
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: result.hits,
});
})
.catch(() =>
dispatchStories({ type: 'STORIES_FETCH_FAILURE' })
);
}, [searchTerm]);
...
};
We changed the feature from a client-side to server-side search. Instead of filtering a predefined list
of stories on the client, the searchTerm is used to fetch a server-side filtered list. The server-side
search happens not only for the initial data fetching, but also for data fetching if the searchTerm
changes. The search feature is fully server-side now.
Caveat: Re-fetching data each time someone types into the input field isn’t optimal though, because
this implementation stresses the API with every keystroke. Hence you might experience API errors
if you trigger requests too often, because most APIs protect themselves against the flood of request
by using so called rate limiting (e.g. allowing only X requests in 1 minute). We will fix this soonish.
Fundamentals of React 126
Exercises:
• Confirm your source code¹⁷⁹.
– Confirm the changes¹⁸⁰.
• Optional: Leave feedback for this section¹⁸¹.
¹⁷⁹https://bit.ly/3lZtjUO
¹⁸⁰https://bit.ly/3b9S4aF
¹⁸¹https://forms.gle/ywE4bFy6D2HSG8Rd7
Fundamentals of React 127
// A
const handleFetchStories = React.useCallback(() => { // B
if (!searchTerm) return;
fetch(`${API_ENDPOINT}${searchTerm}`)
.then((response) => response.json())
.then((result) => {
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: result.hits,
});
})
.catch(() =>
dispatchStories({ type: 'STORIES_FETCH_FAILURE' })
);
}, [searchTerm]); // E
React.useEffect(() => {
handleFetchStories(); // C
}, [handleFetchStories]); // D
...
};
At its core, the application behaves the same, because we have only extracted a function from
Fundamentals of React 128
React’s useEffect Hook. Instead of using the data fetching logic directly in the side-effect, we made
it available as a function for the entire application. The benefit: reusability. The data fetching can
be used by other parts of the application by calling this new function.
However, we have used React’s useCallback Hook to wrap the extracted function, so let’s explore
why it’s needed here. React’s useCallback Hook creates a memoized function every time its
dependency array (E) changes. As a result, the useEffect hook runs again (C), because it depends
on the new function (D):
Visualization
If we didn’t create a memoized function with React’s useCallback Hook, a new handleFetchStories
function would be created each time the App component re-renders, and would be executed in the
useEffect hook to fetch data. The fetched data is then stored as state in the component. Because the
state of the component changed, the component re-renders and creates a new handleFetchStories
function. The side-effect would be triggered to fetch data, and we’d be stuck in an endless loop:
Visualization
1. define: handleFetchStories
2. run: side-effect
3. update: state
4. re-render: component
5. re-define: handleFetchStories
6. run: side-effect
...
React’s useCallback hook changes the function only when one of its values in the dependency array
changes. That’s when we want to trigger a re-fetch of the data, because the input field has new input
and we want to see the new data displayed in our list.
By moving the data fetching function outside the React’s useEffect Hook, it becomes reusable for
other parts of the application. We won’t use it just yet, but it is good use case to understand
the memoized functions in React. Now the useEffect hook runs implicitly when the searchTerm
changes, because the handleFetchStories is re-defined each time the searchTerm changes. Since the
useEffect hook depends on the handleFetchStories, the side-effect for data fetching runs again.
Exercises:
• Confirm your source code¹⁸².
¹⁸²https://bit.ly/3aSpb2v
Fundamentals of React 129
¹⁸³https://bit.ly/3G4vkGX
¹⁸⁴https://www.robinwieruch.de/react-usecallback-hook/
¹⁸⁵https://forms.gle/HSX9aurgsf5j76HR9
Fundamentals of React 130
What’s important with this feature is that we need a state for the fluctuant searchTerm and a new
state for the confirmed search. But first of all, create a new button element which confirms the search
and will execute the data request eventually:
src/App.js
return (
<div>
<h1>My Hacker Stories</h1>
<InputWithLabel
id="search"
value={searchTerm}
isFocused
onInputChange={handleSearchInput}
>
<strong>Search:</strong>
</InputWithLabel>
<button
¹⁸⁶https://bit.ly/2ZaJXI8
Fundamentals of React 131
type="button"
disabled={!searchTerm}
onClick={handleSearchSubmit}
>
Submit
</button>
...
</div>
);
};
Second, we distinguish between the handler of the input field and the button. While the renamed
handler of the input field still sets the stateful searchTerm, the new handler of the button sets the new
stateful value called url which is derived from the current searchTerm and the static API endpoint
as a new state:
src/App.js
...
...
};
Third, instead of running the data fetching side-effect on every searchTerm change (which happens
Fundamentals of React 132
each time the input field’s value changes like we have seen before), the new stateful url is used
whenever a user changes it by confirming a search request when clicking the button:
src/App.js
fetch(url)
.then((response) => response.json())
.then((result) => {
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: result.hits,
});
})
.catch(() =>
dispatchStories({ type: 'STORIES_FETCH_FAILURE' })
);
}, [url]);
React.useEffect(() => {
handleFetchStories();
}, [handleFetchStories]);
...
};
Before the searchTerm was used for two cases: updating the input field’s state and activating the
side-effect for fetching data. Now it’s only used for the former. A second state called url got
introduced for triggering the side-effect that fetches the data which only happens when a user clicks
the confirmation button.
Exercises:
• Confirm your source code¹⁸⁷.
– Confirm the changes¹⁸⁸.
• Question: Why is useState instead of useStorageState used for the url state management?
¹⁸⁷https://bit.ly/3n47Qcw
¹⁸⁸https://bit.ly/3AXZk3J
Fundamentals of React 133
– Answer: We do not want to remember the url in the browser’s local storage, because
it’s already derived from a static string (here: API_ENDPOINT) and the searchTerm which
already comes from the browser’s local storage.
• Question: Why is there no check for an empty searchTerm in the handleFetchStories function
anymore?
– Answer: Preventing a server-side search happens in the new button, because it gets
disabled whenever there is no searchTerm.
• Optional: Leave feedback for this section¹⁸⁹.
¹⁸⁹https://forms.gle/HuJDuVNZmEDbhGzU9
Fundamentals of React 134
...
You can use axios instead of fetch. Its usage looks almost identical to the native fetch API: It takes
the URL as an argument and returns a promise. You don’t have to transform the returned response
to JSON anymore, since axios wraps the result into a data object in JavaScript for you. Just make
sure to adapt your code to the returned data structure:
¹⁹⁰https://bit.ly/3ncFfSs
¹⁹¹https://bit.ly/3ASC86Y
¹⁹²https://bit.ly/3jjEupg
Fundamentals of React 135
src/App.js
axios
.get(url)
.then((result) => {
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: result.data.hits,
});
})
.catch(() =>
dispatchStories({ type: 'STORIES_FETCH_FAILURE' })
);
}, [url]);
...
};
In this code, we call axios axios.get() for an explicit HTTP GET request¹⁹³, which is the same
HTTP method we used by default with the browser’s native fetch API. You can use other HTTP
methods such as HTTP POST with axios.post() as well. We can see with these examples that axios
is a powerful library for performing requests to remote APIs. I recommend it over the native fetch
API when requests become complex, working with older browsers, or for testing.
Exercises:
• Confirm your source code¹⁹⁴.
– Confirm the changes¹⁹⁵.
• Read more about popular libraries in React¹⁹⁶.
• Optional: Read more about axios¹⁹⁷.
• Optional: Leave feedback for this section¹⁹⁸.
¹⁹³https://mzl.la/3n5kUyi
¹⁹⁴https://bit.ly/3piaFt4
¹⁹⁵https://bit.ly/3jjMruF
¹⁹⁶https://www.robinwieruch.de/react-libraries/
¹⁹⁷https://bit.ly/3jjEupg
¹⁹⁸https://forms.gle/wfDb7r5K4az3TiWN9
Fundamentals of React 136
Async/Await in React
There is no way around asynchronous data when working on real world applications. There
will always be a remote API that gives your frontend data, so you need to work with this data
asynchronously. In our React application, we have started to resolve promises with then/catch blocks.
However, in modern JavaScript (and therefore React), a more popular solution is using async/await.
If you are already familiar with async/await or you want to explore its usage¹⁹⁹ yourself, go ahead
and change the code from using then/catch to async/await. If you have come this far, you could also
consider to compensate the removal of the catch block for the error handling by using a try/catch
blocks instead.
Let’s continue with the task here. First, you would have to replace the then/catch syntax with the
async/await syntax. The following refactoring of the handleFetchStories() function shows how to
accomplish it without error handling:
src/App.js
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: result.data.hits,
});
}, [url]);
...
};
To use async/await, our function requires the async keyword. Once you start using the await
keyword on returned promises, everything reads like synchronous code. Actions after the await
keyword are not executed until the promise resolves, meaning the code will wait. To include error
handling as before, the try and catch blocks are there to help. If something goes wrong in the try
block, the code will jump into the catch block to handle the error:
¹⁹⁹https://mzl.la/3AWyWaw
Fundamentals of React 137
src/App.js
try {
const result = await axios.get(url);
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: result.data.hits,
});
} catch {
dispatchStories({ type: 'STORIES_FETCH_FAILURE' });
}
}, [url]);
...
};
After all, using async/await with try/catch over then/catch makes it often more readable, because
we avoid using callback functions and instead try to make our code more readable in a synchronous
way. However, using then/catch is fine too. In the end, the whole team working on a project should
agree on one syntax.
Exercises:
• Confirm your source code²⁰⁰.
– Confirm the changes²⁰¹.
• Read more about data fetching in React²⁰².
• Optional: Leave feedback for this section²⁰³.
²⁰⁰https://bit.ly/3po4jsf
²⁰¹https://bit.ly/3G4t3LV
²⁰²https://www.robinwieruch.de/react-hooks-fetch-data/
²⁰³https://forms.gle/mtMmwrrsiwioZ8GH6
Fundamentals of React 138
Forms in React
There will be no modern application that doesn’t use forms. A form is just a proper vehicle to submit
data via a button from various input controls (e.g. input field, checkbox, radio button, slider). Earlier
we introduced a new button to fetch data explicitly with a button click. We’ll advance its use with a
proper HTML form, which encapsulates the button and input field for the search term with its label.
Forms aren’t much different in React’s JSX than in HTML. We’ll implement it in two refactoring steps
with some HTML/JavaScript. First, wrap the input field and button into an HTML form element:
src/App.js
return (
<div>
<h1>My Hacker Stories</h1>
<form onSubmit={handleSearchSubmit}>
<InputWithLabel
id="search"
value={searchTerm}
isFocused
onInputChange={handleSearchInput}
>
<strong>Search:</strong>
</InputWithLabel>
<hr />
...
</div>
);
};
Instead of passing the handleSearchSubmit() handler to the button, it’s used in the new form
element’s onSubmit attribute. The button receives a new type attribute called submit, which indicates
Fundamentals of React 139
that the form element’s onSubmit handles the click and not the button. Next, since the handler is
used for the form event, it executes preventDefault() additionally on React’s synthetic event. This
prevents the HTML form’s native behavior which would lead to a browser reload:
src/App.js
const App = () => {
...
event.preventDefault();
};
...
};
Now we can execute the search feature with the keyboard’s “Enter” key, because we are using a
form instead of just a standalone button. In the next two steps, we will separate the whole form into
a new SearchForm component. If you want to go ahead yourself, do not hesitate. Anyway, that’s
how the form can gets extracted into its own component:
src/App.js
const SearchForm = ({
searchTerm,
onSearchInput,
onSearchSubmit,
}) => (
<form onSubmit={onSearchSubmit}>
<InputWithLabel
id="search"
value={searchTerm}
isFocused
onInputChange={onSearchInput}
>
<strong>Search:</strong>
</InputWithLabel>
The new component is instantiated in the App component. The App component still manages the
state for the form though, because the state triggers the data request in the App component where
the requested data will eventually get passed as props (here: stories.data) to the List component:
src/App.js
return (
<div>
<h1>My Hacker Stories</h1>
<SearchForm
searchTerm={searchTerm}
onSearchInput={handleSearchInput}
onSearchSubmit={handleSearchSubmit}
/>
<hr />
{stories.isLoading ? (
<p>Loading ...</p>
) : (
<List list={stories.data} onRemoveItem={handleRemoveStory} />
)}
</div>
);
};
Forms aren’t much different in React than in plain HTML. When we have input fields and a button
to submit data from them, we can give our HTML more structure by wrapping it into a form element
with a onSubmit attribute. The button that executes the submission therefore needs the “submit” type
to refer the process to the form element’s handler. After all, it makes it more accessible for keyboard
users as well.
Exercises:
• Confirm your source code²⁰⁴.
²⁰⁴https://bit.ly/3jfEcjd
Fundamentals of React 141
²⁰⁵https://bit.ly/3G43M4I
²⁰⁶https://www.robinwieruch.de/react-preventdefault/
²⁰⁷https://forms.gle/d14Mf7WzetP25jxq5
A Roadmap for React
We’ve reached the middle of the Road to React, and I hope you enjoyed reading it so far. In case you
liked it, it would mean a lot to me if you would share the book with your friends who are interested
in learning React. Also, a review on Amazon²⁰⁸, Google²⁰⁹, or Goodreads²¹⁰ would be very much
appreciated.
So far, you have learned everything that you need to know about React’s fundamentals. With this
knowledge, you could take a break from this book and create a minimal React application yourself.
There will be for sure questions popping up, but you can always seek answers by going through the
fundamentals of this book again. After all, learning React.js (and any other programming language,
framework, library) is best done by getting your hands dirty. My favorite approach: Learn the
fundamentals, learn how to connect to an API, find an API that delivers data which aligns with your
personal interests, and build something with it! Fortifying your knowledge of the fundamentals is
key to what comes next.
Continuing from here, there are several paths you can take for your learning experience. First of all,
you can continue reading this book. At its core, the next sections will primarily touch three aspects:
advanced features of React, organizational topics for every React project, and React’s ecosystem.
You will learn about topics such as React’s legacy (since React is getting older, there are always old
pieces of tech), performance optimizations, folder/file structures of a React project, static types with
TypeScript, and styling in React. However, I kept the selection to a none overwhelming opinionated
minimum, because otherwise this book would be a never ending story. If you have the desire to dive
deeper into various subjects though, I can give you the following material below.
React’s ecosystem is huge. Every year I sum up all the essential yet popular libraries²¹¹ that can be
used in React for various aspects. You can poke around in the list and try different libraries that
could improve your own project. For some of them I have written dedicated tutorial series too, such
as React Router²¹² and React Table Library²¹³. While the former is the most popular routing library
for React, the latter is a library which I open sourced myself to create data tables in React.
Next there is one advanced feature in React that we didn’t touch in the book and will not touch in
the rest of it. I kept it outside because of one reason: it’s an advanced feature which would bloat our
minimal application without really showing what problem it solves. So it would end up being an
premature optimization in the eyes of every intermediate developer or a obstacle in the eyes of every
novice developer. However, it’s an important topic for larger React applications, so I don’t want you
to miss it: React Context. If you want to learn about it, I highly recommend you to read more about
²⁰⁸https://amzn.to/2JHlP42
²⁰⁹https://books.google.de/books/about?id=RRLmDwAAQBAJ
²¹⁰https://www.goodreads.com/book/show/37503118-the-road-to-learn-react
²¹¹https://www.robinwieruch.de/react-libraries
²¹²https://www.robinwieruch.de/react-router/
²¹³https://www.robinwieruch.de/react-table-component/
A Roadmap for React 143
React Context²¹⁴ and React’s useContext Hook²¹⁵ in addition to actually using it for a more advanced
pattern by combining multiple hooks in a real application²¹⁶. After all, you will encounter React’s
Context in every large React application, be it by directly using it or by indirectly using it via a
third-party library that uses React’s Context under the hood.
React comes with many patterns for components too. You have learned about component composi-
tion²¹⁷ and reusable components²¹⁸ already. If you haven’t read the referenced tutorials about these
patterns, I encourage you to catch up with them. There are more patterns though, and covering all
of them here wouldn’t give the patterns justice in a small application. That’s why I covered them,
e.g. React Render Prop Components²¹⁹ and React Higher-Order Components²²⁰, extensively in other
material of mine. With the succession of React Hooks though, these patterns are less used these days,
however, in larger React applications you will most likely encounter them.
Furthermore, you have used create-react-app to bootstrap your project in this book. If you are
interested in setting up a React project from scratch²²¹ yourself, by using tools such as Webpack and
Babel which power modern JavaScript build pipelines, I encourage you to go through the process.
You will learn lots about what’s going on under the hood in a third-party tool like create-react-app.
From here on, you can continue reading the book to learn about an opinionated selection of
React’s ecosystem/legacy, organizational recommendations, and more built-in features of React (e.g.
performance optimizations). At the end of the book, you will find even more sections helping you
to implement advanced features for your current React application. In summary, I hope all of the
prior learnings, the referenced material, plus the following sections of the book help you to become
a great React developer.
²¹⁴https://www.robinwieruch.de/react-context/
²¹⁵https://www.robinwieruch.de/react-usecontext-hook/
²¹⁶https://www.robinwieruch.de/react-state-usereducer-usestate-usecontext/
²¹⁷https://www.robinwieruch.de/react-component-composition/
²¹⁸https://www.robinwieruch.de/react-reusable-components/
²¹⁹https://www.robinwieruch.de/react-render-props/
²²⁰https://www.robinwieruch.de/react-higher-order-components/
²²¹https://www.robinwieruch.de/minimal-react-webpack-babel-setup/
React’s Legacy
React has changed a lot since 2013. The iterations of its library, how React applications are written,
and especially its components have all changed drastically. However, many React applications were
built over the last few years, so not everything was created with the current status quo in mind.
This section of the book covers React’s legacy. However, I won’t cover all that’s considered legacy
in React, because some features have been revamped more than once.
Throughout this section, we will compare a modern React application²²² to its legacy version²²³. We’ll
discover that most differences between modern and legacy React are due to class components versus
function components.
²²²https://bit.ly/3vrKgu9
²²³https://bit.ly/2Z1matY
React’s Legacy 145
return (
<>
<label htmlFor={id}>{children}</label>
<input
id={id}
type={type}
value={value}
onChange={onInputChange}
/>
</>
);
}
}
For a while, class components were the popular choice for writing React applications. Eventually,
function components were added, and both co-existed with their distinct purposes side by side:
²²⁴https://bit.ly/3vxh3xY
React’s Legacy 146
Code Playground
const InputWithLabel = ({
id,
value,
type = 'text',
onInputChange,
children,
}) => (
<>
<label htmlFor={id}>{children}</label>
<input
id={id}
type={type}
value={value}
onChange={onInputChange}
/>
</>
);
If no side-effects and no state were used in legacy apps, we’d use a function component instead of a
class component. Before 2018 – before React Hooks were introduced – React’s function components
couldn’t handle side-effects (useEffect hooks) or state (useState/useReducer hooks). As a result,
these components were known as functional stateless components, there only to input props and
output JSX. To use state or side-effects, it was necessary to refactor from a function component to a
class component. When neither state nor side-effects were used, we used class components or more
commonly the more lightweight function component.
With the addition of React Hooks, function components were granted the same features as class
components, with state and side-effects. And since there was no longer any practical difference
between them, the community chose function components over class components since they are
more lightweight.
Exercises:
• Read more about JavaScript Classes²²⁵.
• Read more about how to refactor from a class component to a function component²²⁶.
• Learn more about a different class component syntax²²⁷ which wasn’t popular but more
effective.
²²⁵https://mzl.la/3vvc2FO
²²⁶https://www.robinwieruch.de/react-hooks-migration/
²²⁷https://bit.ly/3lYzrfT
React’s Legacy 147
²²⁸https://bit.ly/3FXUibf
²²⁹https://www.robinwieruch.de/react-component-types/
²³⁰https://forms.gle/g5qLH1KZ5Y1JE1v57
React’s Legacy 148
this.state = {
searchTerm: 'React',
};
}
render() {
const { searchTerm } = this.state;
return (
<div>
<h1>My Hacker Stories</h1>
<SearchForm
searchTerm={searchTerm}
onSearchInput={() => this.setState({
searchTerm: event.target.value
})}
/>
</div>
);
}
}
If the state has more than one property in its state object, the setState method performs only a
shallow update. Only the properties passed to setState are overwritten, and all other properties in
the state object stay intact. Since state management is important for frontend applications, there was
no way around class components without hooks for function components.
In a React class component, there are two dedicated APIs (this.state and this.setState) to
manage a component’s state. In a function component, React’s useState and useReducer hooks
React’s Legacy 149
handle this. Related items are packed into one state hook, while a class component must use a
general state API. This was one of the major reasons to introduce React Hooks and move away from
class components.
Exercises:
• Write a stateful class component (e.g. Input component where a user can type something into
it and the text displays somewhere else).
• Optional: Leave feedback for this section²³¹.
²³¹https://forms.gle/5UPEsMWSGjWyeaiA9
React’s Legacy 150
Imperative React
In a React function component, React’s useRef Hook is used mostly for imperative programming.
Throughout React’s history, the ref and its usage had a few versions:
The React team introduced React’s createRef with version 16.3 as the latest equivalent to a function
component’s useRef hook which has been integrated with React Hooks in 16.8:
Code Playground
this.inputRef = React.createRef();
}
componentDidMount() {
if (this.props.isFocused) {
this.inputRef.current.focus();
}
}
render() {
...
return (
<>
...
<input
ref={this.inputRef}
id={id}
type={type}
value={value}
onChange={onInputChange}
/>
</>
);
React’s Legacy 151
}
}
With the helper function, the ref is created in the class’ constructor, applied in the JSX for the
ref attribute, and here used in a class component’s lifecycle method (equivalent to a function
component’s useEffect Hook). The ref can also be used elsewhere, like focusing the input field on a
button click.
Where createRef is used in React’s class components, React’s useRef Hook is used in React function
components. As React shifts towards function components, today it’s common practice to use the
useRef hook exclusively to manage refs and apply imperative programming principles.
Exercises:
• Read more about the different ref techniques in React²³².
• Optional: Leave feedback for this section²³³.
²³²https://bit.ly/3jjhCXa
²³³https://forms.gle/xd131YRHy6NdZa7p7
Styling in React
There are many ways to style a React application, and there are lengthy debates about the best
styling strategy and styling approach. We’ll go over a few of these approaches without giving
them too much weight. There will be some pro and con arguments, but it’s mostly a matter of what
fits best for developers and teams.
We will begin React styling with common CSS in React, but then explore two alternatives for more
advanced CSS-in-CSS (CSS Modules) and CSS-in-JS (Styled Components) strategies. CSS Modules
and Styled Components are only two approaches out of many in both groups of strategies. We’ll also
cover how to include scalable vector graphics (SVGs), such as a logo or icons, in our React application.
If you don’t want to build common UI components (e.g. button, dialog, dropdown) from scratch,
you can always pick a popular UI library suited for React²³⁴, which provides these components by
default. However, it is better for learning React if you try building these components before using a
pre-built solution. As a result, we won’t use any of the UI component libraries.
²³⁴https://www.robinwieruch.de/react-libraries/
Styling in React 153
The following styling approaches and SVGs are pre-configured in create-react-app. If you’re in
control of the build tools (e.g. Webpack) by having a custom setup, they might need to be configured
to enable importing CSS or SVG files. Since we are using create-react-app, we can use these files as
assets right away.
Exercises:
• Read more about the different styling strategies and approaches in React²³⁵.
²³⁵https://www.robinwieruch.de/react-css-styling/
Styling in React 154
CSS in React
Common CSS in React is similar to the standard CSS you may have already learned. Each web
application gives HTML elements a class (in React it’s className) attribute that is styled via a CSS
file:
src/App.js
const App = () => {
...
return (
<div className="container">
<h1 className="headline-primary">My Hacker Stories</h1>
<SearchForm
searchTerm={searchTerm}
onSearchInput={handleSearchInput}
onSearchSubmit={handleSearchSubmit}
/>
{stories.isLoading ? (
<p>Loading ...</p>
) : (
<List list={stories.data} onRemoveItem={handleRemoveStory} />
)}
</div>
);
};
The <hr /> was removed because the CSS handles the border in the next steps. We’ll import the CSS
file, which is done with the help of the create-react-app configuration:
src/App.js
import * as React from 'react';
import axios from 'axios';
import './App.css';
This CSS file will define the two (and more) CSS classes we used (and will use) in the App component.
In your src/App.css file, define them like the following:
Styling in React 155
src/App.css
.container {
height: 100vw;
padding: 20px;
color: #171212;
}
.headline-primary {
font-size: 48px;
font-weight: 300;
letter-spacing: 2px;
}
You should see the first stylings taking effect in your application when you start it again. Next, we
will head over to the Item component. Some of its elements receive the className attribute too,
however, we are also using a new styling technique here:
src/App.js
As you can see, we can also use the style attribute for HTML elements. In JSX, style can be passed
as an inline JavaScript object to these attributes. This way we can define dynamic style properties
in JavaScript files rather than mostly static CSS files. This approach is called inline style, which
is useful for quick prototyping and dynamic style definitions. Inline style should be used sparingly,
however, since a separate style definition with a CSS file keeps the JSX more concise.
In your src/App.css file, define the new CSS classes. Basic CSS features are used here, because
advanced CSS features (e.g. nesting) from CSS extensions (e.g. Sass) are not included in this example,
as they are optional configurations²³⁶:
src/App.css
.item {
display: flex;
align-items: center;
padding-bottom: 5px;
}
The button style from the previous component is still missing, so we’ll define a base button style and
two more specific button styles (small and large). One of the button specifications has been already
used, the other will be used in the next steps:
²³⁶https://bit.ly/3E1a2bM
Styling in React 157
src/App.css
.button {
background: transparent;
border: 1px solid #171212;
padding: 5px;
cursor: pointer;
.button:hover {
background: #171212;
color: #ffffff;
}
.button_small {
padding: 5px;
}
.button_large {
padding: 10px;
}
Apart from styling approaches in React, naming conventions (CSS guidelines²³⁷) are a whole other
topic. The last CSS snippet followed BEM rules by defining modifications of a class with an
underscore (_). Choose whatever naming convention suits you and your team. Without further ado,
we will style the next React component:
src/App.js
<button
type="submit"
disabled={!searchTerm}
className="button button_large"
>
Submit
²³⁷https://mzl.la/3m5avnb
Styling in React 158
</button>
</form>
);
We can also pass the className attribute as a prop to React components. For example, we can
use this option to pass the SearchForm component a flexible style with a className prop from a
range of predefined classes (e.g. button_large or button_small) from a CSS file. Lastly, style the
InputWithLabel component:
src/App.js
return (
<>
<label htmlFor={id} className="label">
{children}
</label>
<input
ref={inputRef}
id={id}
type={type}
value={value}
onChange={onInputChange}
className="input"
/>
</>
);
};
src/App.css
.search-form {
padding: 10px 0 20px 0;
display: flex;
align-items: baseline;
}
.label {
border-top: 1px solid #171212;
border-left: 1px solid #171212;
padding-left: 5px;
font-size: 24px;
}
.input {
border: none;
border-bottom: 1px solid #171212;
background-color: transparent;
font-size: 24px;
}
For simplicity, we styled elements like label and input individually in the src/App.css file. However,
in a real application, it may be better to define these elements once in the src/index.css file globally.
As React components are split into multiple files, sharing style becomes a necessity. After all, this
is the basic usage of CSS in React. Without CSS extensions like Sass (Syntactically Awesome Style
Sheets), styling can become more burdensome, though, because features like CSS nesting are not
available in native CSS.
Exercises:
• Confirm your source code²³⁸.
– Confirm the changes²³⁹.
• Read more about CSS stylesheets in create-react-app²⁴⁰.
– Try to pass className prop from App to SearchForm component, either with the value
button_small or button_large, and use this as className for the button element.
• Read more about Sass in create-react-app²⁴¹ for taking advantage of more advanced CSS
features like nesting.
• Enable Sass in your create-react-app and start to use its features (e.g. nesting) in your CSS file.
• Optional: Leave feedback for this section²⁴².
²³⁸https://bit.ly/3jk9llQ
²³⁹https://bit.ly/3vrrnr4
²⁴⁰https://bit.ly/3G0fTjn
²⁴¹https://bit.ly/3GeJGF9
²⁴²https://forms.gle/RovYbjYF9McD1h6c7
Styling in React 160
mv src/App.css src/App.module.css
In the renamed src/App.module.css, start with the first CSS class definitions, as before:
src/App.module.css
.container {
height: 100vw;
padding: 20px;
color: #171212;
}
.headlinePrimary {
font-size: 48px;
font-weight: 300;
letter-spacing: 2px;
}
Import the src/App.module.css file with a relative path again. This time, import it as a JavaScript
object where the name of the object (here: styles) is up to you:
src/App.js
Instead of defining the className as a string mapped to a CSS file, access the CSS class directly from
the styles object, and assign it with a JavaScript in JSX expression to your elements.
Styling in React 161
src/App.js
return (
<div className={styles.container}>
<h1 className={styles.headlinePrimary}>My Hacker Stories</h1>
<SearchForm
searchTerm={searchTerm}
onSearchInput={handleSearchInput}
onSearchSubmit={handleSearchSubmit}
/>
{stories.isLoading ? (
<p>Loading ...</p>
) : (
<List list={stories.data} onRemoveItem={handleRemoveStory} />
)}
</div>
);
};
There are various ways to add multiple CSS classes via the styles object to the element’s single
className attribute. Here, we use JavaScript template literals:
src/App.js
>
Dismiss
</button>
</span>
</li>
);
We can also add inline styles as more dynamic styles in JSX again. It’s also possible to add a CSS
extension like Sass to enable advanced features like CSS nesting (see the previous section). We will
stick to native CSS features though:
src/App.module.css
.item {
display: flex;
align-items: center;
padding-bottom: 5px;
}
.button {
background: transparent;
border: 1px solid #171212;
padding: 5px;
cursor: pointer;
.button:hover {
background: #171212;
color: #ffffff;
}
.buttonSmall {
padding: 5px;
}
.buttonLarge {
padding: 10px;
}
There is a shift toward pseudo BEM naming conventions here, in contrast to button_small and
button_large from the previous section. If the previous naming convention holds true, we can only
access the style with styles['button_small'] which makes it more verbose because of JavaScript’s
limitation with object underscores. The same shortcomings would apply for classes defined with a
dash (-). In contrast, now we can use styles.buttonSmall instead (see: Item component):
src/App.js
<button
type="submit"
disabled={!searchTerm}
className={`${styles.button} ${styles.buttonLarge}`}
>
Submit
</button>
</form>
);
The SearchForm component receives the styles as well. It has to use string interpolation for using
two styles in one element via JavaScript’s template literals. One alternative way is the classnames²⁴³
library, which is installed via the command line as a project dependency:
²⁴³https://bit.ly/3aSz1Bl
Styling in React 164
src/App.js
...
The library offers conditional styling too; whereas the left-hand side of the object’s property must
be used as a computed property name²⁴⁴ and is only applied if the right-hand side evaluates to true:
src/App.js
...
return (
<>
<label htmlFor={id} className={styles.label}>
{children}
</label>
<input
ref={inputRef}
id={id}
type={type}
value={value}
onChange={onInputChange}
className={styles.input}
/>
</>
²⁴⁴https://mzl.la/2XuN651
Styling in React 165
);
};
.label {
border-top: 1px solid #171212;
border-left: 1px solid #171212;
padding-left: 5px;
font-size: 24px;
}
.input {
border: none;
border-bottom: 1px solid #171212;
background-color: transparent;
font-size: 24px;
}
The same caution as the last section applies: some of these styles like input and label might be more
efficient in a global src/index.css file without CSS modules.
Again, CSS Modules – like any other CSS-in-CSS approach – can use Sass for more advanced CSS
features like nesting. The advantage of CSS modules is that we receive an error in JavaScript each
time a style isn’t defined. In the standard CSS approach, unmatched styles in JavaScript and CSS
files might go unnoticed.
Exercises:
• Confirm your source code²⁴⁵.
– Confirm the changes²⁴⁶.
• Read more about CSS Modules in create-react-app²⁴⁷.
• Optional: Leave feedback for this section²⁴⁸.
²⁴⁵https://bit.ly/3lVeoeb
²⁴⁶https://bit.ly/3pkqgs4
²⁴⁷https://bit.ly/3phgN51
²⁴⁸https://forms.gle/iuU7WaeJVwHN2pFCA
Styling in React 166
As the name suggests, CSS-in-JS happens in your JavaScript file. In your src/App.js file, define your
first styled components:
src/App.js
background: #83a4d4;
background: linear-gradient(to left, #b6fbff, #83a4d4);
color: #171212;
`;
When using Styled Components, you are using the JavaScript template literals the same way as
JavaScript functions. Everything between the backticks can be seen as an argument and the styled
object gives you access to all the necessary HTML elements (e.g. div, h1) as functions. Once a function
is called with the style, it returns a React component that can be used in your App component:
Styling in React 167
src/App.js
return (
<StyledContainer>
<StyledHeadlinePrimary>My Hacker Stories</StyledHeadlinePrimary>
<SearchForm
searchTerm={searchTerm}
onSearchInput={handleSearchInput}
onSearchSubmit={handleSearchSubmit}
/>
{stories.isLoading ? (
<p>Loading ...</p>
) : (
<List list={stories.data} onRemoveItem={handleRemoveStory} />
)}
</StyledContainer>
);
};
This kind of React component follows the same rules as a common React component. Everything
passed between its element tags is passed automatically as React children prop. For the Item
component, we are not using inline styles this time, but defining a dedicated styled component
for it. StyledColumn receives its styles dynamically using a React prop:
src/App.js
The flexible width prop is accessible in the styled component’s template literal as an argument of
an inline function. The return value from the function is applied there as a string. Since we can use
immediate returns when omitting the arrow function’s body, it becomes a concise inline function:
src/App.js
a {
color: inherit;
}
Advanced features like CSS nesting are available in Styled Components by default. Nested elements
are accessible and the current element can be selected with the & CSS operator:
Styling in React 169
src/App.js
&:hover {
background: #171212;
color: #ffffff;
}
`;
You can also create specialized versions of styled components by passing another component to the
library’s function. The specialized button receives all the base styles from the previously defined
StyledButton component:
src/App.js
When we use a styled component like StyledSearchForm, its underlying form element is used in the
real HTML output. We can continue using the native HTML attributes (onSubmit, type, disabled)
there:
Styling in React 170
src/App.js
Finally, the InputWithLabel decorated with its yet undefined styled components:
src/App.js
return (
<>
<StyledLabel htmlFor={id}>{children}</StyledLabel>
<StyledInput
ref={inputRef}
id={id}
type={type}
value={value}
onChange={onInputChange}
/>
</>
);
};
And its matching styled components are defined in the same file:
Styling in React 171
src/App.js
font-size: 24px;
`;
CSS-in-JS with styled components shifts the focus of defining styles to actual React components.
Styled Components are styles defined as React components without the intermediate CSS file. If a
styled component isn’t used in a JavaScript, your IDE/editor will tell you. Styled Components are
bundled next to other JavaScript assets in JavaScript files for a production-ready application. There
are no extra CSS files, but only JavaScript when using the CSS-in-JS strategy. Both strategies, CSS-
in-JS and CSS-in-CSS, and their approaches (e.g. Styled Components and CSS Modules) are popular
among React developers. Use what suits you and your team best.
Exercises:
• Confirm your source code²⁴⁹.
– Confirm the changes²⁵⁰.
• Read more about best practices for Styled Components in React²⁵¹.
• Usually there is no src/index.css file for global styles when using Styled Components. Find out
how to use global styles when using Styled Components.
• Optional: Leave feedback for this section²⁵².
²⁴⁹https://bit.ly/2ZaHeOU
²⁵⁰https://bit.ly/3n4aH5e
²⁵¹https://www.robinwieruch.de/styled-components/
²⁵²https://forms.gle/5vFxvg9hSNAna37S8
Styling in React 172
SVGs in React
To create a modern React application, we’ll likely need to use SVGs. Instead of giving every button
element text, for example, we might want to make it lightweight with an icon. In this section, we’ll
use a scalable vector graphic (SVG) as an icon in one of our React components.
This section builds on the “CSS in React” we covered earlier, to give the SVG icon a good look and
feel right away. It’s acceptable to use a different styling approach, or no styling at all, though the
SVG might look off without it.
This icon as SVG is taken from Flaticon’s Freepick²⁵³. Many of the SVGs on this website are free to
use, though they require you to mention the author. You can download the icon from here²⁵⁴ as SVG
and put it in your project as src/check.svg. Downloading the file is the recommended way, however,
for the sake of completion, this is the verbose SVG definition:
Code Playground
Because we’re using create-react-app again, we can import SVGs (similar to CSS) as React compo-
nents right away. In src/App.js, use the following syntax for importing the SVG:
²⁵³https://bit.ly/3E16SEz
²⁵⁴https://bit.ly/2Z2EoeA
Styling in React 173
src/App.js
import './App.css';
import { ReactComponent as Check } from './check.svg';
We are importing an SVG, and this works for many different uses for SVGs (e.g. logo, background).
Instead of a button text, pass the SVG component with a height and width attribute:
src/App.js
Regardless of the styling approach you are using, you can give your SVG icon in the button a hover
effect too. In the basic CSS approach, it would look like the following in the src/App.css file:
src/App.css
The create-react-app project makes using SVGs straightforward, with no extra configuration needed.
This is different if you create a React project from scratch with build tools like Webpack, because
you have to take care of it yourself. Anyway, SVGs make your application more approachable, so
use them whenever it suits you.
Exercises:
• Confirm your source code²⁵⁵.
– Confirm the changes²⁵⁶.
• Read more about SVGs in create-react-app²⁵⁷.
• Read more about SVG background patterns in React²⁵⁸.
• Add another SVG icon in your application.
• Integrate the third-party library react-fontawesome²⁵⁹ or react-icons²⁶⁰ into your application
and use its SVG symbols.
• Optional: Leave feedback for this section²⁶¹.
²⁵⁵https://bit.ly/3DSltSI
²⁵⁶https://bit.ly/3lYaEZf
²⁵⁷https://bit.ly/3DVxBT9
²⁵⁸https://www.robinwieruch.de/react-svg-patterns/
²⁵⁹https://bit.ly/2ZdNXI7
²⁶⁰https://bit.ly/3nayoJ7
²⁶¹https://forms.gle/3yGgMDR2VQ5WksSXA
React Maintenance
Once a React application grows, maintenance becomes a priority. To prepare for this eventuality,
we’ll cover performance optimization, type safety, testing, and project structure. Each of these topics
will strengthen your app to take on more functionality without losing quality.
Performance optimization prevents applications from slowing down by assuring efficient use
of available resource. Typed programming languages like TypeScript detect bugs earlier in the
feedback loop. Testing gives us more explicit feedback than typed programming, and provides a
way to understand which actions can break the application. Lastly, a project structure supports the
organized management of assets into folders and files, which is especially useful in scenarios where
team members work in different domains.
React Maintenance 176
React.useEffect(() => {
console.log('A');
localStorage.setItem(key, value);
}, [value, key]);
With a closer look at the developer’s tools, we can see the log for the first time when the component
renders using this custom hook. It doesn’t make sense to run the side-effect for the initial rendering
of the component though, because there is nothing to store in the local storage except the initial
value. It’s a redundant function invocation, and should only run for every update (re-rendering) of
the component.
As mentioned, there is no React Hook that runs on every re-render, and there is no way to tell the
useEffect hook in a React idiomatic way to call its function only on every re-render. However, by
using React’s useRef Hook which keeps its ref.current property intact over re-renders, we can keep
a made up state (without re-rendering the component on state updates) with an instance variable of
our component’s lifecycle:
React Maintenance 177
src/App.js
React.useEffect(() => {
if (!isMounted.current) {
isMounted.current = true;
} else {
console.log('A');
localStorage.setItem(key, value);
}
}, [value, key]);
We are exploiting the ref and its mutable current property for imperative state management that
doesn’t trigger a re-render. Once the hook is called from its component for the first time (component
render), the ref’s current property is initialized with a false boolean called isMounted. As a result,
the side-effect function in useEffect isn’t called; only the boolean flag for isMounted is toggled to
true in the side-effect. Whenever the hook runs again (component re-render), the boolean flag is
evaluated in the side-effect. Since it’s true, the side-effect function runs. Over the lifetime of the
component, the isMounted boolean will remain true. It was there to avoid calling the side-effect
function for the first time render that uses our custom hook.
The above was only about preventing the invocation of one simple function for a component
rendering for the first time. But imagine you have an expensive computation in your side-effect,
or the custom hook is used frequently in the application. It’s more practical to deploy this technique
to avoid unnecessary function invocations.
Exercises:
• Read more about running useEffect only on update²⁶².
• Read more about running useEffect only once²⁶³.
²⁶²https://www.robinwieruch.de/react-useeffect-only-on-update/
²⁶³https://www.robinwieruch.de/react-useeffect-only-once/
React Maintenance 178
console.log('B:App');
return ( ... );
};
Because the List component has no function body, and developers are lazy folks who don’t want to
refactor the component for a simple logging statement, the List component uses the || operator
instead. This is a neat trick for adding a logging statement to a function component without a
function body. Since the console.log() on the left-hand side of the operator always evaluates to
false, the right-hand side of the operator gets always executed.
Code Playground
function getTheTruth() {
if (console.log('B:List')) {
return true;
} else {
return false;
}
}
React Maintenance 179
console.log(getTheTruth());
// B:List
// false
Let’s focus on the actual logging in the browser’s developer tools. You should see a similar output.
First, the App component renders, followed by its child components (e.g. List component).
Visualization
B:App
B:List
B:App
B:App
B:List
Note: If you are seeing more than these loggings, check whether your *src/index.js file uses
<React.StrictMode> as a wrapper for your App component. If it’s the case, remove the strict mode
and check your logging again. Explanation: In development mode, React’s StrictMode renders a
component twice to detect problems with your implementation in order to warn you about these.
This StrictMode is automatically excluded for applications in production. However, if you don’t
want to be confused by the multiple renders, remove StrictMode from the src/index.js file.*
Since a side-effect triggers data fetching after the first render, only the App component renders,
because the List component is replaced by a loading indicator in a conditional rendering. Once the
data arrives, both components render again.
Visualization
// initial render
B:App
B:List
So far, this behavior is acceptable, since everything renders on time. Now we’ll take this experiment
a step further, by typing into the SearchForm component’s input field. You should see the changes
with every character entered into the element:
React Maintenance 180
Visualization
B:App
B:List
What’s striking is that the List component shouldn’t re-render, but it does. The search feature
isn’t executed via its button, so the list passed to the List component remains the same for every
keystroke. This is React’s default behavior, re-rendering everything below a component with a state
change, which surprises many people. In other words, if a parent component re-renders, its child
components re-render as well. React does this by default, because preventing a re-render of child
components could lead to bugs, and the re-rendering mechanism of React is often fast enough by
default..
Sometimes we want to prevent re-rendering, however. For example, huge data sets displayed in a
table shouldn’t re-render if they are not affected by an update. It’s more efficient to perform an
equality check if something changed for the component. Therefore, we can use React’s memo API
to make this equality check for the props:
src/App.js
const List = React.memo(
({ list, onRemoveItem }) =>
console.log('B:List') || (
<ul>
{list.map((item) => (
<Item
key={item.objectID}
item={item}
onRemoveItem={onRemoveItem}
/>
))}
</ul>
)
);
However, the output stays the same when typing into the SearchForm’s input field:
Visualization
B:App
B:List
That’s because the list passed to the List component is the same, but the onRemoveItem callback
handler isn’t. If the App component re-renders, it always creates a new version of this callback
handler as a new function. Earlier, we used React’s useCallback Hook to prevent this behavior, by
creating a function only on a re-render (if one of its dependencies has changed):
React Maintenance 181
src/App.js
const App = () => {
...
...
console.log('B:App');
return (... );
};
Since the callback handler gets the item passed as an argument in its function signature, it doesn’t
have any dependencies and is declared only once when the App component initially renders. None
of the props passed to the List component should change now. Try it with the combination of React
memo and useCallback, to search via the SearchForm’s input field. The “B:List” logging disappears,
and only the App component re-renders with its “B:App” logging.
While all props passed to a component stay the same, the component renders again if its parent
component is forced to re-render. That’s React’s default behavior, which works most of the
time because the re-rendering mechanism is pretty fast. However, if re-rendering decreases the
performance of a React application, React’s memo API helps prevent re-rendering. As we have seen,
sometimes memo alone doesn’t help, though. Callback handlers are re-defined each time in the parent
component and passed as changed props to the component, which causes another re-render. In
React Maintenance 182
that case, useCallback is used for making the callback handler only change when its dependencies
change.
Exercises:
• Read more about React’s memo API²⁶⁴.
• Read more about React’s useCallback Hook²⁶⁵.
return stories.data.reduce(
(result, value) => result + value.num_comments,
0
);
};
return (
<div>
<h1>My Hacker Stories with {sumComments} comments.</h1>
...
</div>
);
};
If all arguments are passed to a function, it’s acceptable to have it outside the component, because it
does not have any further dependency needed from within the component. This prevents creating
²⁶⁴https://www.robinwieruch.de/react-memo/
²⁶⁵https://www.robinwieruch.de/react-usecallback-hook/
React Maintenance 183
the function on every render, so the useCallback hook becomes unnecessary. However, the function
still computes the value of summed comments on every render, which becomes a problem for more
expensive computations.
Each time text is typed in the input field of the SearchForm component, this computation runs again
with an output of “C”. This may be fine for a non-heavy computation like this one, but imagine this
computation would take more than 500ms. It would give the re-rendering a delay, because everything
in the component has to wait for this computation. We can tell React to only run a function if one of
its dependencies has changed. If no dependency changed, the result of the function stays the same.
React’s useMemo Hook helps us here:
src/App.js
return ( ... );
};
For every time someone types in the SearchForm, the computation shouldn’t run again. It only runs
if the dependency array, here stories, has changed. After all, this should only be used for cost
expensive computations which could lead to a delay of a (re-)rendering of a component.
React Maintenance 184
Now, after we went through these scenarios for useMemo, useCallback, and memo, remember that
these shouldn’t necessarily be used by default. Apply these performance optimizations only if you
run into performance bottlenecks. Most of the time this shouldn’t happen, because React’s rendering
mechanism is pretty efficient by default. Sometimes the check for utilities like memo can be more
expensive than the re-rendering itself.
Exercises:
• Confirm your source code²⁶⁶.
– Confirm the changes²⁶⁷.
• Read more about React’s useMemo Hook²⁶⁸.
• Download React Developer Tools as an extension for your browser. Open it for your application
in the browser via the browser’s developer tools and try its various features. For example, you
can use it to visualize React’s component tree and its updating components.
• Does the SearchForm re-render when removing an item from the List with the “Dismiss”-
button? If it’s the case, apply performance optimization techniques to prevent re-rendering.
• Does each Item re-render when removing an item from the List with the “Dismiss”-button? If
it’s the case, apply performance optimization techniques to prevent re-rendering.
• Remove all performance optimizations to keep the application simple. Our current application
doesn’t suffer from any performance bottlenecks. Try to avoid premature optimzations²⁶⁹. Use
this section and its further reading material as a reference, in case you run into performance
problems.
• Optional: Leave feedback for this section²⁷⁰.
²⁶⁶https://bit.ly/3AYYTpJ
²⁶⁷https://bit.ly/3aVxXgc
²⁶⁸https://www.robinwieruch.de/react-usememo-hook/
²⁶⁹https://bit.ly/3AYktL8
²⁷⁰https://forms.gle/FwNrJSdLikquVzsB6
React Maintenance 185
TypeScript in React
TypeScript for JavaScript and React has many benefits for developing robust applications. Instead
of getting type errors on runtime in the command line or browser, TypeScript integration presents
them during compile time inside the IDE. It shortens the feedback loop for a developer, while it
improves the developer experience. The code becomes more self-documenting and readable, because
every variable is defined with a type. Also moving code blocks or performing a larger refactoring
of a codebase becomes much more efficient. Statically typed languages like TypeScript are trending
because of their benefits over dynamically typed languages like JavaScript. It’s useful to learn more
about TypeScript²⁷¹ whenever possible.
To use TypeScript in React, install TypeScript and its dependencies into your application using the
command line. If you run into obstacles, follow the official TypeScript installation instructions for
create-react-app²⁷²:
Command Line
mv src/index.js src/index.tsx
mv src/App.js src/App.tsx
Restart your development server in the command line. You may encounter compile errors in the
browser and IDE. If the latter doesn’t work, try installing a TypeScript plugin for your editor, or
extension for your IDE. After the initial TypeScript in React setup, we’ll add type safety²⁷³ for the
entire src/App.tsx file, starting with typing the arguments of the custom hook:
²⁷¹https://bit.ly/3G0l3vL
²⁷²https://bit.ly/3DYG880
²⁷³https://bit.ly/3jhm6xi
React Maintenance 186
src/App.tsx
const useStorageState = (
key: string,
initialState: string
) => {
const [value, setValue] = React.useState(
localStorage.getItem(key) || initialState
);
React.useEffect(() => {
localStorage.setItem(key, value);
}, [value, key]);
Adding types to the function’s arguments is more about Javascript than React. We are telling the
function to expect two arguments, which are JavaScript string primitives. Also, we can tell the
function to return an array ([]) with a string (state), and tell functions like the state updater
function that takes a value to return nothing (void):
src/App.tsx
const useStorageState = (
key: string,
initialState: string
): [string, (newValue: string) => void] => {
const [value, setValue] = React.useState(
localStorage.getItem(key) || initialState
);
React.useEffect(() => {
localStorage.setItem(key, value);
}, [value, key]);
Related to React though, considering the previous type safety improvements for the custom hook, we
hadn’t to add types to the internal React hooks in the function’s body. That’s because type inference
works most of the time for React hooks out of the box. If the initial state of a React useState Hook
is a JavaScript string primitive, then the returned current state will be inferred as a string and the
returned state updater function will only take a string as an argument and return nothing:
React Maintenance 187
Code Playground
If adding type safety becomes an aftermath for a React application and its components, there are
multiple ways on how to approach it. We will start with the props and state for the leaf components
of our application. For example, the Item component receives a story (here: item) and a callback
handler function (here: onRemoveItem). Starting out very verbose, we could add the inlined types for
both function arguments as we did before:
src/App.tsx
const Item = ({
item,
onRemoveItem,
}: {
item: {
objectID: string;
url: string;
title: string;
author: string;
num_comments: number;
points: number;
};
onRemoveItem: (item: {
objectID: string;
url: string;
title: string;
author: string;
num_comments: number;
points: number;
}) => void;
}) => (
<li>
...
</li>
);
There are two problems: the code is verbose, and it has duplicates. Let’s get rid of both problems by
defining a custom Story type outside the component, at the top of src/App.js:
React Maintenance 188
src/App.tsx
type Story = {
objectID: string;
url: string;
title: string;
author: string;
num_comments: number;
points: number;
};
...
const Item = ({
item,
onRemoveItem,
}: {
item: Story;
onRemoveItem: (item: Story) => void;
}) => (
<li>
...
</li>
);
The item is of type Story; the onRemoveItem function takes an item of type Story as an argument
and returns nothing. Next, clean up the code by defining the props of the Item component outside:
src/App.tsx
type ItemProps = {
item: Story;
onRemoveItem: (item: Story) => void;
};
That’s the most popular way to type React component’s props with TypeScript. Fortunately, the
return type of the function component is inferred. However, if you want to explicitly use it, you can
do so with JSX.Element. From here, we can navigate up the component tree into the List component
and apply the same type definitions for the props:
React Maintenance 189
src/App.tsx
type Story = {
...
};
...
type ListProps = {
list: Stories;
onRemoveItem: (item: Story) => void;
};
The onRemoveItem function is typed twice for the ItemProps and ListProps. To be more accurate,
you could extract this to a standalone defined OnRemoveItem TypeScript type and reuse it for both
onRemoveItem prop type definitions. Note, however, that development becomes increasingly difficult
as components are split up into different files. That’s why we will keep the duplication here. Now,
since we already have the Story and Stories types, we can repurpose them for other components.
Add the Story type to the callback handler in the App component:
React Maintenance 190
src/App.tsx
...
};
The reducer function manages the Story type as well, without really touching it due to state and
action types. As the application’s developer, we know both objects and their shapes that are passed
to this reducer function:
src/App.tsx
type StoriesState = {
data: Stories;
isLoading: boolean;
isError: boolean;
};
type StoriesAction = {
type: string;
payload: any;
};
const storiesReducer = (
state: StoriesState,
action: StoriesAction
) => {
...
};
The Action type with its string and any (TypeScript wildcard) type definitions are still too broad;
and we gain no real type safety through it, because actions are not distinguishable. We can do
better by specifying each action TypeScript type as an interface, and using a union type (here:
StoriesAction) for the final type safety:
React Maintenance 191
src/App.tsx
interface StoriesFetchInitAction {
type: 'STORIES_FETCH_INIT';
}
interface StoriesFetchSuccessAction {
type: 'STORIES_FETCH_SUCCESS';
payload: Stories;
}
interface StoriesFetchFailureAction {
type: 'STORIES_FETCH_FAILURE';
}
interface StoriesRemoveAction {
type: 'REMOVE_STORY';
payload: Story;
}
type StoriesAction =
| StoriesFetchInitAction
| StoriesFetchSuccessAction
| StoriesFetchFailureAction
| StoriesRemoveAction;
const storiesReducer = (
state: StoriesState,
action: StoriesAction
) => {
...
};
The reducer’s current state, action, and returned state (inferred) are type safe now. For example, if
you would dispatch an action to the reducer with an action type that’s not defined, you would get
a type error. Or if you would pass something else than a story to the action which removes a story,
you would get a type error as well.
Let’s shift our focus to the SearchForm component, which has callback handlers with events:
React Maintenance 192
src/App.tsx
type SearchFormProps = {
searchTerm: string;
onSearchInput: (event: React.ChangeEvent<HTMLInputElement>) => void;
onSearchSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
};
const SearchForm = ({
searchTerm,
onSearchInput,
onSearchSubmit,
}: SearchFormProps) => (
...
);
const handleSearchInput = (
event: React.ChangeEvent<HTMLInputElement>
) => {
setSearchTerm(event.target.value);
};
const handleSearchSubmit = (
event: React.FormEvent<HTMLFormElement>
) => {
setUrl(`${API_ENDPOINT}${searchTerm}`);
event.preventDefault();
};
...
};
All that’s left is the InputWithLabel component. Before handling this component’s props, let’s take
a look at the ref from React’s useRef Hook. Unfortunately, the return value isn’t inferred:
React Maintenance 193
src/App.tsx
React.useEffect(() => {
if (isFocused && inputRef.current) {
inputRef.current.focus();
}
}, [isFocused]);
We made the returned ref type safe, and typed it as read-only because we only execute the focus
method on it (read). React takes over for us there, setting the DOM element to the current property.
Lastly, we will apply type safety checks for the InputWithLabel component’s props. Note the
children prop with its React specific type and the optional types are signaled with a question
mark:
src/App.tsx
type InputWithLabelProps = {
id: string;
value: string;
type?: string;
onInputChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
isFocused?: boolean;
children: React.ReactNode;
};
const InputWithLabel = ({
id,
value,
type = 'text',
onInputChange,
isFocused,
children,
}: InputWithLabelProps) => {
...
};
Both the type and isFocused properties are optional. Using TypeScript, you can tell the compiler
that these don’t need to be passed to the component as props. The children prop has a lot of
TypeScript type definitions that could be applicable to this concept, the most universal of which
is React.ReactNode from the React library.
React Maintenance 194
Our entire React application is finally typed by TypeScript, making it easy to spot type errors on
compile time. When adding TypeScript to your React application, start by adding type definitions
to your function’s arguments. These functions can be vanilla JavaScript functions, custom React
hooks, or React function components. Only when using React is it important to know specific types
for form elements, events, and JSX.
Exercises:
• Confirm your source code²⁷⁴.
– Confirm the changes²⁷⁵.
• Dig into the React + TypeScript Cheatsheet²⁷⁶, because most common use cases we faced in
this section are covered there as well. There is no need to know everything from the top of
your head.
• While you continue with the learning experience in the following sections, remove or keep
your types with TypeScript. If you do the latter, add new types whenever you get a compile
error.
• Optional: Leave feedback for this section²⁷⁷.
²⁷⁴https://bit.ly/3vtF8p4
²⁷⁵https://bit.ly/3pjxlJr
²⁷⁶https://bit.ly/3phdf2H
²⁷⁷https://forms.gle/Pyw2oUjXV85hwk2t6
React Maintenance 195
Testing in React
Testing source code is an essential part of programming and should be seen as a mandatory exercise
for serious developers. The goal is to verify our source code’s quality and functionality before using
it in production. The testing pyramid²⁷⁸ will serve as our guide.
The testing pyramid includes end-to-end tests, integration tests, and unit tests. Unit tests are for
small, isolated blocks of code, such as a single function or component. Integration tests help us figure
out how well these blocks of code work together. An end-to-end test simulates a real-life scenario,
like a user logging into a web application. Unit tests are quick and easy to write and maintain;
end-to-end tests are the opposite.
Many unit tests are required to cover all the functions and components in a working application, after
which several integration tests make sure that the most important units work together. Finally, a few
end-to-end tests to simulate critical user scenarios. In this learning experience, we will cover unit
and integration tests, in addition to a useful component-specific testing technique called snapshot
tests. E2E tests will be part of the exercise.
Choosing a testing library can be a challenge for React beginners, as there are many options. To
keep things simple, we’ll employ the most popular tools: Jest²⁷⁹ and React Testing Library²⁸⁰ (RTL).
Jest is a full-blown testing framework with test runners, test suites, test cases, and assertions. RTL
is used for rendering React components, triggering events like mouse clicks, and selecting HTML
elements from the DOM to perform assertions. We’ll explore both tools step-by-step, from setup to
unit testing to integration testing.
The “describe” block is our test suite, and the “test” blocks are our test cases. Note that test cases can
be used without test suites, which may apply to code outside the scope of this lesson:
src/App.test.js
Large subjects like functions or components often require multiple test cases, so it makes sense to
use them with test suites:
src/App.test.js
});
});
});
Note that a “test” block can also be written as an “it” block. The blocks have the same purpose, except
the “it” block may be more familiar to programmers from other programming languages:
React Maintenance 197
src/App.test.js
Fortunately, create-react-app comes with Jest, so you don’t need to install anything. You can run
tests using the test script from your package.json in the command line as soon as create-react-app
is installed, and then execute your tests with npm test to produce the following output:
Command Line
PASS src/App.test.js
something truthy and falsy
✓ true to be true (3ms)
✓ false to be false (1ms)
Watch Usage
› Press a to run all tests.
› Press f to run only failed tests.
› Press q to quit watch mode.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press Enter to trigger a test run.
When we run the test command, the test runner matches all files with a test.js suffix. Successful tests
are displayed in green, failed tests in red. The interactive test script watches your tests and source
code and executes tests when the files change. Jest also provides a few interactive commands, such
as pressing “f” to run failed tests and “a” for running all tests. Let’s see how this looks for a failed
test:
React Maintenance 198
src/App.test.js
The tests run again, and the command line output shows a failed test in red:
Command Line
FAIL src/App.test.js
something truthy and falsy
✓ true to be true (2ms)
� false to be false (4ms)
Expected: true
Received: false
5 |
6 | test('false to be false', () => {
> 7 | expect(false).toBe(true);
| ^
8 | });
9 | });
10 |
at Object.<anonymous> (src/App.test.js:7:19)
Familiarize yourself with this test output, because it shows all failed tests, as well as information
on why they failed. Using this information, you can fix certain parts of your code until all tests run
green. Next, we’ll cover test assertions, two of which we’ve already used with Jest’s expect function.
An assertion works by expecting value on the left side (expect) to match a value on the right side
(toBe). toBe is only one of many available assertive functions provided by Jest.
src/App.test.js
Once you start testing, it’s a good practice to keep two command line interfaces open: one for
watching your tests (npm test), and one for developing your application (npm start). Also, source
control platforms like Git may require an additional command line interface for adding your source
code to the repository.
Exercises:
• Confirm your source code²⁸¹.
– Confirm the changes²⁸².
• Read more about Jest²⁸³.
²⁸¹https://bit.ly/3C42rII
²⁸²https://bit.ly/3B2CesG
²⁸³https://jestjs.io
React Maintenance 200
src/App.js
...
The exercises at the end of this chapter will cover all the remaining tests you should consider
performing. For now, we can import all the components and reducers in our src/App.test.js file
and we will focus on the reducer test first. We are also importing React here, because we have to
include it whenever we test React components:
src/App.test.js
import App, {
storiesReducer,
Item,
List,
SearchForm,
InputWithLabel,
} from './App';
Before we unit test our first React component, we’ll cover how to test just a JavaScript function.
The best candidate for this test use case is the storiesReducer function and one of its actions. Let’s
define some test data and the test suite for the reducer test:
src/App.test.js
...
const storyOne = {
title: 'React',
url: 'https://reactjs.org/',
author: 'Jordan Walke',
num_comments: 3,
points: 4,
objectID: 0,
};
React Maintenance 201
const storyTwo = {
title: 'Redux',
url: 'https://redux.js.org/',
author: 'Dan Abramov, Andrew Clark',
num_comments: 2,
points: 5,
objectID: 1,
};
describe('storiesReducer', () => {
test('removes a story from all stories', () => {
});
});
If you extrapolate the test cases, there should be one test case per reducer action. We will focus on
a single action, which you can use to perform the rest as exercise yourself. The reducer function
accepts a state and an action, and then returns a new state, so reducer tests follow the same pattern:
src/App.test.js
...
describe('storiesReducer', () => {
test('removes a story from all stories', () => {
const action = // TODO: some action
const state = // TODO: some current state
expect(newState).toBe(expectedState);
});
});
For our case, we define action, state, and expected state according to our reducer. The expected state
will have one less story, which was removed as it passed to the reducer as action:
React Maintenance 202
src/App.test.js
describe('storiesReducer', () => {
test('removes a story from all stories', () => {
const action = { type: 'REMOVE_STORY', payload: storyOne };
const state = { data: stories, isLoading: false, isError: false };
const expectedState = {
data: [storyTwo],
isLoading: false,
isError: false,
};
expect(newState).toBe(expectedState);
});
});
This test still fails because we are using toBe instead of toStrictEqual. The toBe assertive function
makes a strict comparison like newState === expectedState. The content of the objects are the
same, however, their object references are not the same. We use toStrictEqual instead of toBe to
limit our comparison to the object’s content:
src/App.test.js
describe('storiesReducer', () => {
test('removes a story from all stories', () => {
const action = { type: 'REMOVE_STORY', payload: storyOne };
const state = { data: stories, isLoading: false, isError: false };
const expectedState = {
data: [storyTwo],
isLoading: false,
isError: false,
};
expect(newState).toStrictEqual(expectedState);
});
});
There is always the decision to make for JavaScript objects whether you want to make a strict
comparison or just a content comparison. Most often you only want to have a content comparison
React Maintenance 203
here, hence use toStrictEqual. For JavaScript primitives though, like strings or booleans, you can
still use toBe. Also note that there is a toEqual function which works slightly different²⁸⁴ than
toStrictEqual.
We continue to make adjustments until the reducer test turns green, which is really testing a
JavaScript function with a certain input and expecting a certain output. We haven’t done any testing
methods regarding React yet.
Remember, a reducer function will always follow the same test pattern: given a state and action,
we expect the following new state. Every action of the reducer could be another test case in our
reducer’s test suite, so consider using the exercises as a way to move through your entire source
code.
Exercises:
• Confirm your source code²⁸⁵.
– Confirm the changes²⁸⁶.
• Continue to write a test case for every reducer action and its state transition.
• Read more about Jest’s assertive functions like toBe and toStrictEqual.
...
²⁸⁴https://bit.ly/3jlPpii
²⁸⁵https://bit.ly/3jjIhD3
²⁸⁶https://bit.ly/3E175aO
React Maintenance 204
Start with the Item component, where we assert whether it renders all expected properties based on
its given props. Based on the input (props), we are asserting an output (rendered HTML). We’ll use
RTL’s render function in each test to render a React component. In this case, we render the Item
component as an element and pass it an item object – one of our previously defined stories – as
props:
src/App.test.js
describe('Item', () => {
test('renders all properties', () => {
render(<Item item={storyOne} />);
});
});
After rendering it, we can use the debug function from RTL’s screen object:
src/App.test.js
describe('Item', () => {
test('renders all properties', () => {
render(<Item item={storyOne} />);
screen.debug();
});
});
Run the tests with npm test, and you’ll see the output from the debug function. It prints all your
component’s and child component’s HTML elements. The output should be similar to the following:
src/App.test.js
<body>
<div>
<li>
<span>
<a
href="https://reactjs.org/"
>
React
</a>
</span>
<span>
Jordan Walke
</span>
<span>
React Maintenance 205
3
</span>
<span>
4
</span>
<span>
<button
type="button"
>
Dismiss
</button>
</span>
</li>
</div>
</body>
Here you should form the habit of using RTL’s debug function whenever you render a new
component in a React component test. The function gives a useful overview of what is rendered
and informs the best way to proceed with testing. Based on the current output, we can start with
our first assertion. RTL’s screen object provides a function called getByText, one of many search
functions:
src/App.test.js
describe('Item', () => {
test('renders all properties', () => {
render(<Item item={storyOne} />);
expect(screen.getByText('Jordan Walke')).toBeInTheDocument();
expect(screen.getByText('React')).toHaveAttribute(
'href',
'https://reactjs.org/'
);
});
});
For the two assertions, we use the two assertive functions toBeInTheDocument and toHaveAttribute.
These are to verify an element with the text “Jordan Walke” is in the document, and the presence of
an element with the text “React” with a specific href attribute value. Over time, you will see more
of these assertive functions being used.
RTL’s getByText search function finds the one element with the visible texts “Jordan Walke” and
“React”. We can use the getAllByText equivalent to find more than one element. Similar equivalents
exist for other search functions.
React Maintenance 206
The getByText function returns the element with a text that users can see, which relates to the real-
world use of the application. Note that getByText is not the only search function, though. Another
highly-used search function is the getByRole or getAllByRole function:
src/App.test.js
describe('Item', () => {
test('renders all properties', () => {
...
});
expect(screen.getByRole('button')).toBeInTheDocument();
});
});
The getByRole function is usually used to retrieve elements by aria-label attributes²⁸⁷. However,
there are also implicit roles on HTML elements²⁸⁸ – like button for a button element. Thus you
can select elements not only by visible text, but also by their (implicit) accessibility role with React
Testing Library. A neat feature of getRoleBy is that it suggests roles if you provide a role that’s not
available²⁸⁹. Both, getByText and getByRole are RTL’s most widely used search functions.
We can continue here by asserting not only that everything is in the document, but also by asserting
whether our events work as expected. For example, the Item component’s button element can be
clicked and we want to verify that the callback handler gets called. Therefore, we are using Jest for
creating a mocked function which we provide as a callback handler to the Item component. Then,
after firing a click event with React Testing Library on the button, we want to assert that the callback
handler function has been called:
src/App.test.js
describe('Item', () => {
test('renders all properties', () => {
...
});
²⁸⁷https://mzl.la/3B3bBDP
²⁸⁸https://mzl.la/3n7SgN7
²⁸⁹https://bit.ly/3pnPXrQ
React Maintenance 207
fireEvent.click(screen.getByRole('button'));
expect(handleRemoveItem).toHaveBeenCalledTimes(1);
});
});
Jest lets us pass a test-specific function to the Item component as a prop. These test-specific functions
are called spy, stub, or mock; each is used for different test scenarios. The jest.fn() returns us a
mock for the actual function, which lets us capture when it’s called. As a result, we can use Jest
assertions like toHaveBeenCalledTimes, which lets us assert a number of times the function has
been called; and toHaveBeenCalledWith, to verify arguments that are passed to it.
Every time we want to spy a JavaScript function, whether it has been called or whether it received
certain arguments, we can use Jest’s helper function to create a mocked function. Then, after
invoking this function implicitly with RTL’s fireEvent object’s function, we can assert that the
provided callback handler – which is the mocked function – has been called one time.
In the last exercise we tested the Item component’s input and output via rendering assertions and
callback handler assertions. We are not testing real state changes yet, however, as there is no actual
item removed from the DOM after clicking the “Dismiss”-button. The logic to remove the item from
the list is in the App component, but we are only testing the Item component in isolation. Sometimes
it’s just useful to test whether a single block works, before testing everything all together. We will
test the actual implementation logic for removing an Item when we cover the App component later.
For now, the SearchForm component will use the InputWithLabel component as a child component.
As before, we will start by rendering the component and providing all the essential props:
src/App.test.js
describe('SearchForm', () => {
const searchFormProps = {
searchTerm: 'React',
onSearchInput: jest.fn(),
onSearchSubmit: jest.fn(),
};
screen.debug();
React Maintenance 208
});
});
Again, we start with the debugging. After evaluating what renders, we can make the first assertion
for the SearchForm. With input fields, the getByDisplayValue search function is the perfect
candidate to return the input field as an element:
src/App.test.js
describe('SearchForm', () => {
const searchFormProps = { ... };
expect(screen.getByDisplayValue('React')).toBeInTheDocument();
});
});
Since the input element is rendered with a default value, we can use the default value (here: “React”),
which is the displayed value in our test assertion. If the input element doesn’t have a default value,
the application could show a placeholder with the placeholder HTML attribute on the input field.
Then we’d use another function from RTL called getByPlaceholderText, which is used for searching
an element with a placeholder text.
Because the debug information presented multiple options to query the HTML, we could continue
with one more test to assert the rendered label:
src/App.test.js
describe('SearchForm', () => {
const searchFormProps = { ... };
expect(screen.getByLabelText(/Search/)).toBeInTheDocument();
});
});
React Maintenance 209
The getByLabelText search function allows us to find an element by a label in a form. This is useful
for components that render multiple labels and HTML controls. However, you may have noticed
we used a regular expression²⁹⁰ here. If we used a string instead, the colon for “Search:” must be
included. By using a regular expression, we are matching strings that include the “Search” string,
which makes finding elements much more efficient. For this reason, you may find yourself using
regular expressions instead of strings quite often.
Anyway, perhaps it would be more interesting to test the interactive parts of the SearchForm
component. Since our callback handlers, which are passed as props to the SearchForm component,
are already mocked with Jest, we can assert whether these functions get called appropriately:
src/App.test.js
describe('SearchForm', () => {
const searchFormProps = {
searchTerm: 'React',
onSearchInput: jest.fn(),
onSearchSubmit: jest.fn(),
};
...
fireEvent.change(screen.getByDisplayValue('React'), {
target: { value: 'Redux' },
});
expect(searchFormProps.onSearchInput).toHaveBeenCalledTimes(1);
});
fireEvent.submit(screen.getByRole('button'));
expect(searchFormProps.onSearchSubmit).toHaveBeenCalledTimes(1);
});
});
Similar to the Item component, we tested input (props) and output (callback handler) for the Search-
Form component. The difference is that the SearchForm component renders a child component called
²⁹⁰https://mzl.la/3CdDjiZ
React Maintenance 210
InputWithLabel. If you check the debug output, you’ll likely notice that the React Testing Library
doesn’t bother with this child component. This happens because the end-user wouldn’t care about
the component either, so the React Testing Library outputs all the HTML that matters for the test.
All the callback handler tests for Item and SearchForm component test only whether the functions
have been called. No React re-rendering occurs, because all the components are tested in isolation
without state management, which solely happens in the App component. Real testing with RTL starts
further up the component tree, where state changes and side-effects can be evaluated. Therefore, let
me introduce integration testing next.
Exercises:
• Confirm your source code²⁹¹.
– Confirm the changes²⁹².
• Read more about React Testing Library²⁹³.
– Read more about search functions²⁹⁴.
• Add tests for your List and InputWithLabel components.
...
jest.mock('axios');
...
Next, implement the data you want to be returned from the mocked API request with a JavaScript
Promise, and use it for the axios mock. Afterward, we can render our component and assume the
correct data is mocked for our API request:
²⁹¹https://bit.ly/2Z3Vc54
²⁹²https://bit.ly/3E28IoZ
²⁹³https://bit.ly/30KueQH
²⁹⁴https://bit.ly/3jjUw2t
React Maintenance 211
src/App.test.js
describe('App', () => {
test('succeeds fetching data', () => {
const promise = Promise.resolve({
data: {
hits: stories,
},
});
render(<App />);
screen.debug();
});
});
Now we’ll use React Testing Library’s act helper function to wait until the promise resolves after
the component’s initial render. With async/await, we can implement this like synchronous code.
The debug function from RTL is useful because it outputs the App component’s elements before and
after the request:
src/App.test.js
describe('App', () => {
test('succeeds fetching data', async () => {
const promise = Promise.resolve({
data: {
hits: stories,
},
});
render(<App />);
screen.debug();
screen.debug();
});
});
React Maintenance 212
In the debug’s output, we see the loading indicator renders for the first debug function, but not the
second. This is because the data fetching and component re-render complete after we resolve the
promise in our test with act. Let’s assert the loading indicator for this case:
src/App.test.js
describe('App', () => {
test('succeeds fetching data', async () => {
const promise = Promise.resolve({
data: {
hits: stories,
},
});
render(<App />);
expect(screen.queryByText(/Loading/)).toBeInTheDocument();
expect(screen.queryByText(/Loading/)).toBeNull();
});
});
Because we’re testing for a returned element that is absent, this time we use RTL’s queryByText
instead of the getByText function. Using getByText in this instance would produce an error, because
the element can’t be found; but with queryByText the value just returns null.
Again, we’re using a regular expression /Loading/ instead of a string 'Loading'. To use a string,
we’d have to explicitly use 'Loading ...' instead of 'Loading'. With a regular expression, we
don’t need to provide the whole string, we just need to match a part of it.
Next, we can assert whether or not our fetched data gets rendered as expected:
React Maintenance 213
src/App.test.js
describe('App', () => {
test('succeeds fetching data', async () => {
const promise = Promise.resolve({
data: {
hits: stories,
},
});
render(<App />);
expect(screen.queryByText(/Loading/)).toBeInTheDocument();
expect(screen.queryByText(/Loading/)).toBeNull();
expect(screen.getByText('React')).toBeInTheDocument();
expect(screen.getByText('Redux')).toBeInTheDocument();
expect(screen.getAllByText('Dismiss').length).toBe(2);
});
});
The happy path²⁹⁵ for the data fetching is tested now. Similarly, we can test the unhappy path in case
of a failed API request. The promise needs to reject and the error should be caught with a try/catch
block:
src/App.test.js
describe('App', () => {
test('succeeds fetching data', async () => {
...
});
render(<App />);
²⁹⁵https://bit.ly/3jiAbuB
React Maintenance 214
expect(screen.getByText(/Loading/)).toBeInTheDocument();
try {
await act(() => promise);
} catch (error) {
expect(screen.queryByText(/Loading/)).toBeNull();
expect(screen.queryByText(/went wrong/)).toBeInTheDocument();
}
});
});
There may be some confusion about when to use getBy or the queryBy search variants. As a rule of
thumb, use getBy for single elements, and getAllBy for multiple elements. If you are checking for
elements that aren’t present, use queryBy (or queryAllBy). In this code, I preferred using queryBy for
the sake of alignment and readability.
Now we know the initial data fetching works for our App component, so we can move to testing
user interactions. We have only tested user actions in the child components thus far, by firing events
without any state and side-effect. Next, we’ll remove an item from the list after the data has been
fetched successfully. Since the item with “Jordan Walke” is the first rendered item in the list, it gets
removed if we click the first “Dismiss”-button:
src/App.test.js
describe('App', () => {
...
render(<App />);
expect(screen.getAllByText('Dismiss').length).toBe(2);
expect(screen.getByText('Jordan Walke')).toBeInTheDocument();
React Maintenance 215
fireEvent.click(screen.getAllByText('Dismiss')[0]);
expect(screen.getAllByText('Dismiss').length).toBe(1);
expect(screen.queryByText('Jordan Walke')).toBeNull();
});
});
To test the search feature, we set up the mocking differently, because we’re handling initial request,
plus another request once the user searches for more stories by a specific search term:
src/App.test.js
describe('App', () => {
...
const anotherStory = {
title: 'JavaScript',
url: 'https://en.wikipedia.org/wiki/JavaScript',
author: 'Brendan Eich',
num_comments: 15,
points: 10,
objectID: 3,
};
axios.get.mockImplementation((url) => {
if (url.includes('React')) {
return reactPromise;
}
React Maintenance 216
if (url.includes('JavaScript')) {
return javascriptPromise;
}
throw Error();
});
});
});
Instead of mocking the request once with Jest (mockImplementationOnce), now we mock multiple
requests (mockImplementation). Depending on the incoming URL, the request either returns the
initial list (“React”-related stories), or the new list (“JavaScript”-related stories). If we provide an
incorrect URL to the request, the test throws an error for confirmation. As before, let’s render the
App component:
src/App.test.js
describe('App', () => {
...
axios.get.mockImplementation((url) => {
...
});
// Initial Render
render(<App />);
expect(screen.queryByDisplayValue('React')).toBeInTheDocument();
expect(screen.queryByDisplayValue('JavaScript')).toBeNull();
expect(screen.queryByText('Jordan Walke')).toBeInTheDocument();
React Maintenance 217
expect(
screen.queryByText('Dan Abramov, Andrew Clark')
).toBeInTheDocument();
expect(screen.queryByText('Brendan Eich')).toBeNull();
});
});
We are resolving the first promise for the initial render. We expect the input field to render “React”,
and the two items in the list to render the creators of React and Redux. We also make sure that
no stories related to JavaScript are rendered yet. Next, change the input field’s value by firing an
event, and asserting that the new value is rendered from the App component through all its child
components in the actual input field:
src/App.test.js
describe('App', () => {
...
...
expect(screen.queryByText('Jordan Walke')).toBeInTheDocument();
expect(
screen.queryByText('Dan Abramov, Andrew Clark')
).toBeInTheDocument();
expect(screen.queryByText('Brendan Eich')).toBeNull();
fireEvent.change(screen.queryByDisplayValue('React'), {
target: {
value: 'JavaScript',
},
});
expect(screen.queryByDisplayValue('React')).toBeNull();
expect(
screen.queryByDisplayValue('JavaScript')
).toBeInTheDocument();
});
});
React Maintenance 218
Lastly, we can submit this search request by firing a submit event with the button. The new search
term is used from the App component’s state, so the new URL searches for JavaScript-related stories
that we have mocked before:
src/App.test.js
describe('App', () => {
...
...
expect(screen.queryByDisplayValue('React')).toBeNull();
expect(
screen.queryByDisplayValue('JavaScript')
).toBeInTheDocument();
fireEvent.submit(screen.queryByText('Submit'));
expect(screen.queryByText('Jordan Walke')).toBeNull();
expect(
screen.queryByText('Dan Abramov, Andrew Clark')
).toBeNull();
expect(screen.queryByText('Brendan Eich')).toBeInTheDocument();
});
});
Brendan Eich is rendered as the creator of JavaScript, while the creators of React and Redux are
removed. This test depicts an entire test scenario in one test case. We can move through each step –
initial fetching, changing the input field value, submitting the form, and retrieving new data from
the API – with the tools we’ve used.
React Testing Library with Jest is the most popular library combination for React testing. RTL
provides relevant testing tools, while Jest has a general testing framework for test suites, test cases,
assertions, and mocking capabilities. If you need an alternative to RTL, consider trying Enzyme²⁹⁶
by Airbnb.
²⁹⁶https://www.robinwieruch.de/react-testing-jest-enzyme/
React Maintenance 219
Exercises:
• Confirm your source code²⁹⁷.
– Confirm the changes²⁹⁸.
• Read more about React Testing Library in React²⁹⁹.
• Read more about E2E tests in React³⁰⁰.
• While you continue with the upcoming sections, keep your tests green and add new tests when
needed.
Snapshot Testing
Facebook created snapshot tests as a more lightweight way to test React components and their
structure. A snapshot test creates an instance of your rendered component’s output as HTML
elements and their structure. This snapshot is compared to the same snapshot in the next test to
give more output on how the rendered component changed and show why any tests failed in the
difference. You can accept or deny any differences in your source code until the component functions
as intended.
Snapshot tests are lightweight, with less focus on the implementation details of the component. Let’s
perform a snapshot test for our SearchForm component:
src/App.test.js
describe('SearchForm', () => {
...
Run these tests with npm test and you’ll see a new src/_snapshots_ folder has been created in your
project folder. Similar to RTL’s debug function, there’s a snapshot of your rendered SearchForm
component as an HTML element structure in the file. Next, head to src/App.js file and change the
HTML. For example, try removing the bold text from the SearchForm component:
²⁹⁷https://bit.ly/3DOSIqe
²⁹⁸https://bit.ly/3jf3pKM
²⁹⁹https://www.robinwieruch.de/react-testing-library/
³⁰⁰https://www.robinwieruch.de/react-testing-cypress/
React Maintenance 220
src/App.js
const SearchForm = ({
searchTerm,
onSearchInput,
onSearchSubmit,
}) => (
<form onSubmit={onSearchSubmit}>
<InputWithLabel
id="search"
value={searchTerm}
isFocused
onInputChange={onSearchInput}
>
Search:
</InputWithLabel>
After the next test, the command line should look similar to the following:
Command Line
- Snapshot
+ Received
<label
for="search"
>
- <strong>
- Search:
- </strong>
+ Search:
</label>
This is a typical case for a breaking snapshot test. When a component’s HTML structure is changed
unintentionally, the snapshot test informs us in the command line. To fix it, we’d go into the
src/App.js file and edit the SearchForm component. For intentional changes, press “u” on the
command line for interactive tests and a new snapshot will be created. Try it and see how the
snapshot file in your src/_snapshots_ folder changes.
React Maintenance 221
Jest stores snapshots in a folder so it can validate the difference against future snapshot tests. Users
can share these snapshots across teams using version control platforms like git. This is how we make
sure the DOM stays the same.
Snapshot tests are useful for setting up tests quickly in React, though it’s best to avoid using them
exclusively. Instead, use snapshot tests for components that don’t update often, are less complex,
and where it’s easier to compare component results.
Exercises:
• Confirm your source code³⁰¹.
– Confirm the changes³⁰².
• Add one snapshot test for each of all the other components and check the content of your
_snapshots_ folder.
• Optional: Leave feedback for this section³⁰³.
³⁰¹https://bit.ly/3G7tF3M
³⁰²https://bit.ly/3lUYpge
³⁰³https://forms.gle/tMJyXvxS1AmRvSUU9
React Maintenance 222
Move every component from the src/App.js file in its own file, except for the List component which
has to share its place with the Item component in the src/List.js file. Then in every file make sure
to import React and to export the component which needs to be used from the file. For example, in
src/List.js file:
src/List.js
³⁰⁴https://www.robinwieruch.de/javascript-import-export/
React Maintenance 223
export { List };
Since only the List component uses the Item component, we can keep it in the same file. If this
changes because the Item component is used elsewhere, we can give the Item component its own
file. The SearchForm component in the src/SearchForm.js file must import the InputWithLabel
component. Like the Item component, we could have left the InputWithLabel component next to the
SearchForm; but our goal is to make InputWithLabel component reusable with other components:
src/SearchForm.js
import * as React from 'react';
const SearchForm = ({
searchTerm,
onSearchInput,
onSearchSubmit,
}) => (
<form onSubmit={onSearchSubmit}>
<InputWithLabel
id="search"
value={searchTerm}
isFocused
onInputChange={onSearchInput}
>
<strong>Search:</strong>
</InputWithLabel>
React Maintenance 224
export { SearchForm };
The App component has to import all the components it needs to render. It doesn’t need to import
InputWithLabel, because it’s only used for the SearchForm component.
src/App.js
...
Components that are used in other components now have their own file. If a component should be
used as a reusable component (e.g. InputWithLabel), it receives its own file. Only if a component
(e.g. Item) is dedicated to another component (e.g. List) do we keep it in the same file. From here,
there are several strategies to structure your folder/file hierarchy. One scenario is to create a folder
for every component:
React Maintenance 225
Project Structure
- List/
-- index.js
- SearchForm/
-- index.js
- InputWithLabel/
-- index.js
The index.js file holds the implementation details for the component, while other files in the same
folder have different responsibilities like styling, testing, and types:
Project Structure
- List/
-- index.js
-- style.css
-- test.js
-- types.js
If using CSS-in-JS, where no CSS file is needed, one could still have a separate style.js file for all the
styled components:
Project Structure
- List/
-- index.js
-- style.js
-- test.js
-- types.js
If you scale this to the deeper level folder structure, each component will have its own folder in a
domain-oriented project structure as well:
React Maintenance 226
Project Structure
- Messages/
-- index.js
-- style.css
-- test.js
-- types.js
- Users/
-- index.js
-- style.css
-- test.js
-- types.js
- shared/
-- Button/
--- index.js
--- style.css
--- test.js
--- types.js
-- Input/
--- index.js
--- style.css
--- test.js
--- types.js
There are many ways on how to structure your React project from small to large project: simple
to complex folder structure; one-level nested to two-level nested folder nesting; dedicated folders
for styling, types, and testing next to implementation logic. There is no right way for folder/file
structures. However, in the exercises, you will find my 5 steps approach to structure a React project.
After all, a project’s requirements evolve over time and so should its structure. If keeping all assets
in one file feels right, then there is no rule against it.
Exercises:
• Confirm your source code³⁰⁵.
– Confirm the changes³⁰⁶.
• Read more about JavaScript’s import and export statements³⁰⁷.
• Read more about React Folder Structures³⁰⁸.
• Keep the current folder structure if you feel confident. The ongoing sections will omit it, only
using the src/App.js file.
• Optional: Leave feedback for this section³⁰⁹.
³⁰⁵https://bit.ly/2XxzDcG
³⁰⁶https://bit.ly/3DW8109
³⁰⁷https://www.robinwieruch.de/javascript-import-export/
³⁰⁸https://www.robinwieruch.de/react-folder-structure/
³⁰⁹https://forms.gle/yLzszsmtdB1DQBCe7
Real World React (Advanced)
We’ve covered most of React’s fundamentals, its legacy features, and techniques for maintaining
applications. Now it’s time to dive into developing real-world React features. Each of the following
sections will come with a task. Try to tackle these tasks without the optional hints first, but be aware
that these are going to be challenging on your first attempt. If you need help, use the optional hints
or follow the instructions from the section.
Real World React (Advanced) 228
Sorting
Task: Working with a list of items often includes interactions that make data more approachable
by users. So far, every item was listed with each of its properties. To make it explorable, the list
should enable the sorting of each property by title, author, comments, and points in ascending or
descending order. Sorting in only one direction is fine, because sorting in the other direction will be
part of the next task.
Optional Hints:
Okay, let’s tackle this task! We will treat the list of data like a table. Each row represents an item of
the list and each column represents one property of the item. Introducing headers should provide
the user more guidance about each column:
³¹⁰https://lodash.com
Real World React (Advanced) 229
src/App.js
{list.map((item) => (
<Item
key={item.objectID}
item={item}
onRemoveItem={onRemoveItem}
/>
))}
</ul>
);
We are using inline style for the most basic layout. To match the layout of the header with the rows,
give the rows in the Item component a layout as well:
src/App.js
In the ongoing implementation, we will remove the style attributes, because it takes up lots of space
and clutters the actual implementation logic (hence extracting it into proper CSS). But I encourage
you to keep it for yourself.
The List component will handle the new sort state. This can also be done in the App component,
but in the end, only the List component needs it, so we can lift the state directly to it. The sort state
initializes with a 'NONE' state, so the list items are displayed in the order they are fetched from the
API. Furthermore, we will add a new handler to set the sort state with a sort-specific key:
src/App.js
return (
...
);
};
In the List component’s header, buttons can help us to set the sort state for each column/property.
An inline handler is used to sneak in the sort-specific key (sortKey). When the button for the “Title”
column is clicked, 'TITLE' becomes the new sort state:
src/App.js
return (
<div>
<div>
<span>
<button type="button" onClick={() => handleSort('TITLE')}>
Title
</button>
</span>
<span>
<button type="button" onClick={() => handleSort('AUTHOR')}>
Author
</button>
</span>
Real World React (Advanced) 231
<span>
<button type="button" onClick={() => handleSort('COMMENT')}>
Comments
</button>
</span>
<span>
<button type="button" onClick={() => handleSort('POINT')}>
Points
</button>
</span>
<span>Actions</span>
</div>
The state management for the new feature is implemented, but we don’t see anything when our
buttons are clicked yet. This happens because the sorting mechanism hasn’t been applied to the
actual list. Sorting an array with JavaScript isn’t trivial, because every JavaScript primitive (e.g.
string, boolean, number) comes with edge cases when an array is sorted by its properties. We will
use a library called Lodash³¹¹ to solve this, which comes with many JavaScript utility functions (e.g.
sortBy). First, install it via the command line:
Command Line
npm install lodash
Second, at the top of your file, import the utility function for sorting:
src/App.js
import * as React from 'react';
import axios from 'axios';
import { sortBy } from 'lodash';
...
Third, create a JavaScript object (also called dictionary in this case) with all the possible sortKey and
sort function mappings. Each specific sort key is mapped to a function that sorts the incoming list.
Sorting by 'NONE' returns the unsorted list; sorting by 'POINT' returns a list and its items sorted by
the points property, and so on:
³¹¹https://lodash.com
Real World React (Advanced) 232
src/App.js
const SORTS = {
NONE: (list) => list,
TITLE: (list) => sortBy(list, 'title'),
AUTHOR: (list) => sortBy(list, 'author'),
COMMENT: (list) => sortBy(list, 'num_comments').reverse(),
POINT: (list) => sortBy(list, 'points').reverse(),
};
With the sort (sortKey) state and all possible sort variations (SORTS) at our disposal, we can sort the
list before mapping it:
src/App.js
return (
<div>
...
{sortedList.map((item) => (
<Item
key={item.objectID}
item={item}
onRemoveItem={onRemoveItem}
/>
))}
</div>
);
};
Real World React (Advanced) 233
Task’s done and here comes the recap: First we extracted the sort function from the dictionary by
its sortKey (state), then we applied the function to the list before mapping it to render each Item
component. Second, we rendered HTML buttons as header columns to give our users interaction.
Then, we added implementation details for each button by changing the sort state. Finally, we used
the sort state to sort the actual list.
Exercises:
• Confirm your source code³¹².
– Confirm the changes³¹³.
• Read more about Lodash³¹⁴.
• Why did we use numeric properties like points and num_comments for a reverse sort?
• Use your styling skills to give the user feedback about the current active sort. This mechanism
can be as straightforward as giving the active sort button a different color.
• Optional: Leave feedback for this section³¹⁵.
³¹²https://bit.ly/3aUfX5W
³¹³https://bit.ly/2ZfbVTb
³¹⁴https://lodash.com
³¹⁵https://forms.gle/GM71SDZZWPQWEwmB7
Real World React (Advanced) 234
Reverse Sort
Task: The sort feature works, but the ordering only includes one direction. Implement a reverse sort
when a sort button is clicked twice, so it becomes a toggle between normal (ascending) and reverse
(descending) sort.
Optional Hints:
• Consider that reverse or normal sort could be just another state (e.g. isReverse) next to the
sortKey.
• Set the new state in the handleSort handler based on the previous sort.
• Use the new isReverse state for sorting the list with the sort function from the dictionary with
the optionally applied reverse() function from JavaScript arrays.
Let’s get to the task. The initial sort direction works for strings, as well as numeric sorts like the
reverse sort for JavaScript numbers that arranges them from high to low. Now we need another
state to track whether the sort is reversed or normal:
src/App.js
...
};
Next, give the sort handler logic to see if the incoming sortKey triggers are a normal or reverse sort.
If the sortKey is the same as the one in the state, it should be a reverse sort, but only if the sort state
wasn’t already reversed:
src/App.js
return (
...
);
};
Lastly, depending on the new isReverse state, apply the sort function from the dictionary with or
without the built-in JavaScript reverse method for arrays:
src/App.js
return (
...
);
};
The reverse sort is now operational! Congratulations, you have a fully sortable list now. And by the
way: For the object passed to the state updater function, we use what is called a shorthand object
initializer notation:
Real World React (Advanced) 236
src/App.js
const user = {
firstName: firstName,
};
console.log(user);
// { firstName: "Robin" }
When the property name in your object is the same as your variable name, you can omit the
key/value pair and just write the name:
src/App.js
const user = {
firstName,
};
console.log(user);
// { firstName: "Robin" }
Exercises:
• Confirm your source code³¹⁷.
– Confirm the changes³¹⁸.
• Consider the drawback of keeping the sort state in the List instead of the App component. If
you don’t know, sort the list by “Title” and search for other stories afterward. What would be
different if the sort state would be in the App component.
• Use your styling skills to give the user feedback about the current active sort and its reverse
state. It could be an arrow up or arrow down SVG³¹⁹ next to each active sort button.
• Optional: Leave feedback for this section³²⁰.
³¹⁶https://mzl.la/2XuN651
³¹⁷https://bit.ly/3lVe7Ib
³¹⁸https://bit.ly/3jerTE3
³¹⁹https://bit.ly/3lXfLZN
³²⁰https://forms.gle/ZoJSHFJf2swcBHXM6
Real World React (Advanced) 237
• Don’t use a new state for this feature. Instead, reuse the url state and setUrl state updater
function to fetch stories from the API. Adapt them to multiple urls as state, and to set multiple
urls with setUrls. The last URL from urls can be used to fetch the data, and the last five URLs
from urls can be used to display the buttons.
Let’s get to it. First, we will refactor all url to urls state and all setUrl to setUrls state updater
functions. Instead of initializing the state with an url as a string, make it an array with the initial
url as its only entry:
src/App.js
const App = () => {
...
...
};
Second, instead of using the current url state for data fetching, use the last url entry from the urls
array. If another url is added to the list of urls, it is used to fetch data instead:
Real World React (Advanced) 238
src/App.js
...
try {
const lastUrl = urls[urls.length - 1];
const result = await axios.get(lastUrl);
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: result.data.hits,
});
} catch {
dispatchStories({ type: 'STORIES_FETCH_FAILURE' });
}
}, [urls]);
...
};
And third, instead of storing the url string as state with the state updater function, concatenate the
new url using the concat method with the previous urls in an array for the new state:
src/App.js
event.preventDefault();
};
...
};
Real World React (Advanced) 239
With each search, another URL is stored in our state of urls. Next, render a button for each of the
last five URLs. We’ll include a new universal handler for these buttons, and each passes a specific
url with a more specific inline handler:
src/App.js
...
return (
<div>
<h1>My Hacker Stories</h1>
{lastSearches.map((url) => (
<button
key={url}
type="button"
onClick={() => handleLastSearch(url)}
>
{url}
</button>
))}
...
</div>
);
};
Next, instead of showing the whole URL of the last search in the button as button text, show only
the search term by replacing the API’s endpoint with an empty string:
Real World React (Advanced) 240
src/App.js
...
return (
<div>
...
{lastSearches.map((searchTerm) => (
<button
key={searchTerm}
type="button"
onClick={() => handleLastSearch(searchTerm)}
>
{searchTerm}
</button>
))}
...
</div>
);
};
The getLastSearches function now returns search terms instead of URLs. The actual searchTerm is
passed to the inline handler instead of the url. By mapping over the list of urls in getLastSearches,
we can extract the search term for each url within the array’s map method. Making it more concise,
it can also look like this:
Real World React (Advanced) 241
src/App.js
Now we’ll provide functionality for the new handler used by every button, since clicking one of
these buttons should trigger another search. Since we use the urls state for fetching data, and since
we know the last URL is always used for data fetching, concatenate a new url to the list of urls to
trigger another search request:
src/App.js
...
};
If you compare this new handler’s implementation logic to the handleSearchSubmit, you may
see some common functionality. Extract this common functionality to a new handler and a new
extracted utility function:
src/App.js
...
event.preventDefault();
};
};
...
};
The new utility function can be used somewhere else in the App component. If you extract
functionality that can be used by two parties, always check to see if it can be used by a third-party:
src/App.js
...
};
The functionality should work, but it complains or breaks if the same search term is used more
than once, because searchTerm is used for each button element as key attribute. Make the key more
specific by concatenating it with the index of the mapped array.
src/App.js
return (
<div>
...
</button>
))}
...
</div>
);
};
It’s not the perfect solution, because the index isn’t a stable key (especially when adding items to
the list); however, it doesn’t break in this scenario. The feature works now, but you can add further
UX improvements by following the tasks below.
More Tasks:
• (1) Do not show the current search as a button, only the five preceding searches. Hint: Adapt
the getLastSearches function.
• (2) Don’t show duplicated searches. Searching twice for “React” shouldn’t create two different
buttons. Hint: Adapt the getLastSearches function.
• (3) Set the SearchForm component’s input field value with the last search term if one of the
buttons is clicked.
The source of the five rendered buttons is the getLastSearches function. There, we take the array
of urls and return the last five entries from it. Now we’ll change this utility function to return the
last six entries instead of five by removing the last one, in order to not show the current search as a
button. Afterward, only the five previous searches are displayed as buttons:
src/App.js
If the same search is executed two or more times in a row, duplicate buttons appear, which is likely
not your desired behavior. It would be acceptable to group identical searches into one button if they
followed each other. We will solve this problem in the utility function as well. Before separating the
array into the five previous searches, group the identical searches:
Real World React (Advanced) 244
src/App.js
if (index === 0) {
return result.concat(searchTerm);
}
The reduce function starts with an empty array as its result. The first iteration concatenates the
searchTerm we extracted from the first url into the result. Every extracted searchTerm is compared
to the one before it. If the previous search term is different from the current, concatenate the
searchTerm to the result. If the search terms are identical, return the result without adding anything.
The SearchForm component’s input field should be set with the new searchTerm if one of the last
search buttons is clicked. We can solve this using the state updater function for the specific value
used in the SearchForm component.
src/App.js
handleSearch(searchTerm);
};
...
};
Real World React (Advanced) 245
Lastly, extract the feature’s new rendered content from this section as a standalone component, to
keep the App component lightweight:
src/App.js
const App = () => {
...
return (
<div>
...
<LastSearches
lastSearches={lastSearches}
onLastSearch={handleLastSearch}
/>
<hr />
...
</div>
);
};
This feature wasn’t an easy one. Lots of fundamental React but also JavaScript knowledge was
needed to accomplish it. If you had no problems implementing it yourself or in following the
Real World React (Advanced) 246
instructions, you are very well set. If you had one or the other issue, don’t worry too much about
it. Maybe you even figured out another way to solve this task and it may have turned out simpler
than the one I showed here.
Exercises:
• Confirm your source code³²¹.
– Confirm the changes³²².
• Read more about grouping in JavaScript³²³.
• Optional: Leave feedback for this section³²⁴.
³²¹https://bit.ly/3vtAYgZ
³²²https://bit.ly/3jlnHm1
³²³https://www.robinwieruch.de/javascript-groupby/
³²⁴https://forms.gle/LhNVodZgu8qTqHhN6
Real World React (Advanced) 247
Paginated Fetch
Searching for popular stories via Hacker News API is only one step towards a fully functional search
engine, and there are many ways to fine-tune the search. Take a closer look at the data structure
and observe how the Hacker News API³²⁵ returns more than a list of hits. Specifically, it returns a
paginated list. The page property, which is 0 in the first response, can be used to fetch more paginated
lists as results. You only need to pass the next page with the same search term to the API.
The following shows how to implement a paginated fetch with the Hacker News data structure. If
you are used to pagination from other applications, you may have a row of buttons from 1-10 in
your mind – where the currently selected page is highlighted 1-[3]-10 and where clicking one of the
buttons leads to fetching and displaying this subset of data.
In contrast, we will implement the feature as infinite pagination. Instead of rendering a single
paginated list on a button click, we will render all paginated lists as one list with one button to fetch
the next page. Every additional paginated list is concatenated at the end of the one list.
³²⁵https://hn.algolia.com/api
Real World React (Advanced) 248
Task: Rather than fetching only the first page of a list, extend the functionality for fetching
succeeding pages. Implement this as infinite pagination on button click.
Optional Hints:
• Extend the API_ENDPOINT with the parameters needed for the paginated fetch.
• Store the page from the result as state after fetching the data.
• Fetch the first page (0) of data with every search.
• Fetch the succeeding page (page + 1) for every additional request triggered with a new HTML
button.
Let’s do this! First, extend the API constant so it can deal with paginated data later. We will turn
this one constant:
src/App.js
src/App.js
Fortunately, we don’t need to adjust the API endpoints at other places of the application, because
we extracted a common getUrl function for it. However, there is one spot where we must address
this logic for the future:
src/App.js
In the next steps, it won’t be sufficient to replace the base of our API endpoint, which is no longer
in our code. With more parameters for the API endpoint, the URL becomes more complex. It will
change from X to Y:
src/App.js
// X
https://hn.algolia.com/api/v1/search?query=react
// Y
https://hn.algolia.com/api/v1/search?query=react&page=0
It’s better to extract the search term by extracting everything between ? and &. Also consider that
the query parameter is directly after the ? and all other parameters like page follow it:
src/App.js
The key (query=) also needs to be replaced, leaving only the value (searchTerm):
Real World React (Advanced) 250
src/App.js
const extractSearchTerm = (url) =>
url
.substring(url.lastIndexOf('?') + 1, url.lastIndexOf('&'))
.replace(PARAM_SEARCH, '');
Essentially, we’ll trim the string until we leave only the search term:
src/App.js
// url
https://hn.algolia.com/api/v1/search?query=react&page=0
Next, the returned result from the Hacker News API delivers us the page data:
src/App.js
const App = () => {
...
try {
const lastUrl = urls[urls.length - 1];
const result = await axios.get(lastUrl);
dispatchStories({
type: 'STORIES_FETCH_SUCCESS',
payload: {
list: result.data.hits,
page: result.data.page,
},
});
} catch {
dispatchStories({ type: 'STORIES_FETCH_FAILURE' });
}
}, [urls]);
Real World React (Advanced) 251
...
};
...
};
Extend the API endpoint with the new page parameter. This change was supported by our premature
optimizations earlier, when we extracted the search term from the URL:
Real World React (Advanced) 252
src/App.js
Next, we must adjust all getUrl invocations by passing the page argument. Since the initial search
and the last search always fetch the first page (0), we pass this page as an argument to the function
for retrieving the appropriate URL:
src/App.js
...
event.preventDefault();
};
handleSearch(searchTerm, 0);
};
...
};
Real World React (Advanced) 253
To fetch the next page when a button is clicked, we’ll need to increment the page argument in this
new handler:
src/App.js
...
return (
<div>
...
{stories.isLoading ? (
<p>Loading ...</p>
) : (
<List list={stories.data} onRemoveItem={handleRemoveStory} />
)}
We’ve implemented data fetching with the dynamic page argument. The initial and last searches
always use the first page, and every fetch with the new “More”-button uses an incremented page.
There is one crucial bug when trying the feature, though: the new fetches don’t extend the previous
list, but completely replace it.
Real World React (Advanced) 254
We solve this in the reducer by avoiding the replacement of current data with new data, concate-
nating the paginated lists:
src/App.js
const storiesReducer = (state, action) => {
switch (action.type) {
case 'STORIES_FETCH_INIT':
...
case 'STORIES_FETCH_SUCCESS':
return {
...state,
isLoading: false,
isError: false,
data:
action.payload.page === 0
? action.payload.list
: state.data.concat(action.payload.list),
page: action.payload.page,
};
case 'STORIES_FETCH_FAILURE':
...
case 'REMOVE_STORY':
...
default:
throw new Error();
}
};
The displayed list grows after fetching more data with the new button. However, there is still a
Real World React (Advanced) 255
flicker straining the UX. When fetching paginated data, the list disappears for a moment because
the loading indicator appears and reappears after the request resolves.
The desired behavior is to render the list – which is an empty list in the beginning – and replace the
“More”-button with the loading indicator only for the next requests. This is a common UI refactoring
for conditional rendering when the task evolves from a single list to paginated lists:
src/App.js
return (
<div>
...
{stories.isLoading ? (
<p>Loading ...</p>
) : (
<button type="button" onClick={handleMore}>
More
</button>
)}
</div>
);
};
It’s possible to fetch ongoing data for popular stories now. When working with third-party APIs, it’s
always a good idea to explore its API surface. Every remote API returns different data structures, so
its features may vary.
Real World React (Advanced) 256
Exercises:
• Confirm your source code³²⁶.
– Confirm the changes³²⁷.
• Revisit the Hacker News API documentation³²⁸: Is there a way to fetch more items in a list for
a page by just adding further parameters to the API endpoint?
• Revisit the beginning of this section which speaks about pagination and infinite pagination.
How would you implement a normal pagination component with buttons from 1-[3]-10, where
each button fetches and displays only one page of the list?
• Instead of having one “More”-button, how would you implement infinite pagination with an
infinite scroll technique? Rather than clicking a button for fetching the next page explicitly, the
infinite scroll could fetch the next page once the viewport of the browser hits the bottom of
the displayed list.
• Optional: Leave feedback for this section³²⁹.
³²⁶https://bit.ly/3plSBym
³²⁷https://bit.ly/3vu8OT1
³²⁸https://hn.algolia.com/api
³²⁹https://forms.gle/maPsfzHLba8gheCQA
Deploying a React Application
Now it’s time to get out into the world with your React application. There are many ways to deploy
a React application to production, and many competing providers that offer this service. We’ll keep
it simple here by narrowing it down on one provider, after which you’ll be equipped to check out
other hosting providers on your own.
Deploying a React Application 258
Build Process
So far, everything we’ve done has been the development stage of the application, when the
development server handles everything: packaging all files to one application and serving it on
localhost on your local machine. As a result, our code isn’t available for anyone else.
The next step is to take your application to the production stage by hosting it on a remote server,
called deployment, making it accessible for users of your application. Before an application can
go public, it needs to be packaged as one essential application. Redundant code, testing code, and
duplications are removed. There is also a process called minification at work which reduces the code
size once more.
Fortunately, optimizations and packaging, also called bundling, comes with the build tools in create-
react-app. First, build your application on the command line:
Command Line
npm run build
This creates a new build/ folder in your project with the bundled application. You could take this
folder and deploy it on a hosting provider now, but we’ll use a local server to mimic this process
before engaging in the real thing. First, install an HTTP server on your machine:
Command Line
npm install -g http-server
After entering one of the commands, a URL is presented that provides access to your optimized,
packaged and hosted application. It’s sent through a local IP address that can be made available
over your local network, meaning we’re hosting the application on our local machine.
Exercises:
• Optional: Leave feedback for this section³³⁰.
³³⁰https://forms.gle/hFsut8q7eYsWfYL7A
Deploying a React Application 259
Deploy to Firebase
After we’ve built a full-fledged application in React, the final step is deployment. It is the tipping
point of getting your ideas into the world, from learning how to code to producing applications. We
will use Firebase Hosting for deployment.
Firebase works for create-react-app, as well as most libraries and frameworks like Angular and Vue.
First, install the Firebase CLI globally to the node modules:
Command Line
Using a global installation of the Firebase CLI lets us deploy applications without concern over
project dependency. For any globally-installed node package, remember to update it to a newer
version with the same command as often as you can:
Command Line
If you don’t have a Firebase project yet, sign up for a Firebase account³³¹ and create a new project
there. Then you can associate the Firebase CLI with the Firebase account (Google account):
Command Line
firebase login
A URL will display in the command line that you can open in a browser, or the Firebase CLI opens
it. Choose a Google account to create a Firebase project, and give Google the necessary permissions.
Return to the command line to verify a successful login.
Next, move to the project’s folder and execute the following command, which initializes a Firebase
project for the Firebase hosting features:
Command Line
firebase init
Next, choose the Hosting option. If you’re interested in using another tool next to Firebase Hosting,
add other options:
³³¹https://console.firebase.google.com
Deploying a React Application 260
Command Line
? Which Firebase CLI features do you want to set up for this folder? Press Space to \
select features, then Enter to confirm your choices.
? Database: Deploy Firebase Realtime Database Rules
? Firestore: Deploy rules and create indexes for Firestore
? Functions: Configure and deploy Cloud Functions
-> Hosting: Configure and deploy Firebase Hosting sites
? Storage: Deploy Cloud Storage security rules
Google becomes aware of all Firebase projects associated with an account after login, and we can
select one from the Firebase platform:
Command Line
There are a few other configuration steps to define. Instead of using the default public/ folder, we
want to use the build/ folder from create-react-app. If you set up the bundling with a tool like
Webpack yourself, you can choose the appropriate name for the build folder:
Command Line
The create-react-app application creates a build/ folder after we perform the npm run build for the
first time. The folder contains all the merged content from the public/ folder and the src/ folder.
Since it is a single page application, we want to redirect the user to the index.html file, so the React
router can handle client-side routing.
Now your Firebase initialization is complete. This step created a few configuration files for Firebase
Hosting in your project’s folder. You can read more about them in Firebase’s documentation³³² for
configuring redirects, a 404 page, or headers. Finally, deploy your React application with Firebase in
the command line:
³³²https://bit.ly/3DVgbpG
Deploying a React Application 261
Command Line
firebase deploy
After a successful deployment, you should see a similar output with your project’s identifier:
Command Line
Visit both pages to observe the results. The first link navigates to your Firebase project’s dashboard,
where you’ll see a new panel for the Firebase Hosting. The second link navigates to your deployed
React application.
If you see a blank page for your deployed React application, make sure the public key/value pair
in the firebase.json is set to build, or whichever name you chose for this folder. Second, verify
you’ve run the build script for your React app with npm run build. Finally, check out the official
troubleshoot area for deploying create-react-app applications to Firebase³³³. Try another deployment
with firebase deploy.
Exercises:
• Read more about Firebase Hosting³³⁴.
• Connect your domain to your Firebase deployed application³³⁵.
• Optional: If you want to have a managed cloud server, check out DigitalOcean³³⁶. It’s more
work, but it allows more control. I host all my websites, web applications, and backend APIs
there³³⁷.
• Optional: Leave feedback for this section³³⁸.
³³³https://bit.ly/2ZaLMoE
³³⁴https://bit.ly/3lXypAC
³³⁵https://bit.ly/3phFxdp
³³⁶https://m.do.co/c/fb27c90322f3
³³⁷https://www.robinwieruch.de/deploy-applications-digital-ocean/
³³⁸https://forms.gle/QPjydK8UbaXkxCEj9
Outline
We’ve reached the end of the Road to React, and I hope you enjoyed reading it, and that it helped
you gain some traction in React. If you liked the book, share it with your friends who are interested
in learning more about React. Also, a review on Amazon³³⁹, Google³⁴⁰, or Goodreads³⁴¹ would be
very much appreciated.
From here, I recommend you extend the application to create your own React projects before
engaging another book, course, or tutorial. Try it for a week, take it to production by deploying
it, and reach out to me or others to showcase it. I am always interested in seeing what my readers
built, and learning how I can help them along.
If you’re looking for extensions for your application, I recommend several learning paths after you’ve
mastered the fundamentals:
• Routing: You can implement routing for your application with React Router³⁴². There is only
one page in the application we’ve created, but that will grow. React Router helps manage
multiple pages across multiple URLs. When you introduce routing to your application, no
requests are made to the web server for the next page. The router handles this client-side.
• Connecting to a Database and/or Authentication: Growing React applications will eventu-
ally require persistent data. The data should be stored in a database so that keeps it intact
after browser sessions, to be shared with different users. Firebase is one of the simplest ways
to introduce a database without writing a backend application. In my book titled “The Road
to Firebase”³⁴³, you will find a step-by-step guide on how to use Firebase authentication and
database in React.
• Connecting to a Backend: React handles frontend applications, and we’ve only requested
data from a third-party backend’s API thus far. You can also introduce an API with a backend
application that connects to a database and manages authentication/authorization. In “The
Road to GraphQL”³⁴⁴, I teach you how to use GraphQL for client-server communication. You’ll
learn how to connect your backend to a database, how to manage user sessions, and how to
talk from a frontend to your backend application via a GraphQL API.
• State Management: You have used React to manage local component state exclusively in this
learning experience. It’s a good start for most applications, but there are also external state
management solutions for React. I explore the most popular one in my book “The Road to
Redux”³⁴⁵.
³³⁹https://amzn.to/2JHlP42
³⁴⁰https://books.google.de/books/about?id=RRLmDwAAQBAJ
³⁴¹https://www.goodreads.com/book/show/37503118-the-road-to-learn-react
³⁴²https://www.robinwieruch.de/react-router/
³⁴³https://www.roadtofirebase.com/
³⁴⁴https://www.roadtographql.com/
³⁴⁵https://www.roadtoredux.com/
Outline 263
• Tooling with Webpack and Babel: We used create-react-app to set up the application in this
book. At some point you may want to learn the tooling around it, to create projects without
create-react-app. I recommend a minimal setup with Webpack³⁴⁶, after which you can apply
additional tooling.
• Code Organization: Recall the chapter about code organization and apply these changes, if
you haven’t already. It will help organize your components into structured files and folders,
and it will help you understand the principles of code splitting, reusability, maintainability, and
module API design. Your application will grow and need structured modules eventually; so it’s
better to start now.
• Testing: We only scratched the surface of testing. If you are unfamiliar with testing web
applications, dive deeper into unit testing and integration testing³⁴⁷, especially with React
Testing Library for unit/integration testing and Cypress for end-to-end testing in React.
• Type Checking: Earlier we used TypeScript in React, which is good practice to prevent bugs
and improve the developer experience. Dive deeper into this topic to make your JavaScript
applications more robust. Maybe you’ll end up using TypeScript instead of JavaScript all along.
• UI Components: Many beginners introduce UI component libraries like Material UI too early
in their projects. It is more practical to use a dropdown, checkbox, or dialog in React with
standard HTML elements. Most of these components will manage their own local state. A
checkbox has to know whether it is checked or unchecked, so you should implement them
as controlled components. After you cover the basic implementations of these crucial UI
components, introducing a UI component library should become easier.
• React Native: React Native³⁴⁸ brings your application to mobile devices like iOS and Android.
Once you’ve mastered React, the learning curve for React Native shouldn’t be that steep, as they
share the same principles. The only difference with mobile devices are the layout components,
the build tools, and the APIs of your mobile device.
I invite you to visit my website³⁴⁹ to find more interesting topics about web development and
software engineering. You can also subscribe to my Newsletter³⁵⁰ or Twitter page³⁵¹ to get updates
about articles, books, and courses. If you have only the book and want to extend it to the course,
check out the official course website³⁵².
Thank you for reading the Road to React.
Regards,
Robin Wieruch
³⁴⁶https://www.robinwieruch.de/minimal-react-webpack-babel-setup/
³⁴⁷https://www.robinwieruch.de/react-testing-tutorial/
³⁴⁸https://facebook.github.io/react-native/
³⁴⁹https://www.robinwieruch.de
³⁵⁰https://www.getrevue.co/profile/rwieruch
³⁵¹https://twitter.com/rwieruch
³⁵²https://www.roadtoreact.com/