Learning React: Modern Patterns for Developing React Apps, 2nd Edition (eBook PDF) instant download
Learning React: Modern Patterns for Developing React Apps, 2nd Edition (eBook PDF) instant download
https://ebookluna.com/product/learning-react-modern-patterns-for-
developing-react-apps-2nd-edition-ebook-pdf/
Learning React: Modern Patterns for Developing React Apps, 2nd Edition
(eBook PDF)
https://ebookluna.com/product/learning-react-modern-patterns-for-
developing-react-apps-2nd-edition-ebook-pdf/
https://ebookluna.com/product/ebook-pdf-instructional-patterns-strategies-
for-maximizing-student-learning/
(eBook PDF) Reflections: Patterns for Reading and Writing 2nd Edition
https://ebookluna.com/product/ebook-pdf-reflections-patterns-for-reading-
and-writing-2nd-edition/
https://ebookluna.com/product/ebook-pdf-patterns-of-modern-chinese-history-
by-charles-a-desnoyers/
Modern Sample Preparation for Chromatography, 2nd Edition Serban Moldoveanu
- eBook PDF
https://ebookluna.com/download/modern-sample-preparation-for-
chromatography-2nd-edition-ebook-pdf/
(Original PDF) Learning for Teaching, Teaching for Learning with Student
Resour
https://ebookluna.com/product/original-pdf-learning-for-teaching-teaching-
for-learning-with-student-resour/
(eBook PDF) Developing Content Area Literacy: 40 Strategies for Middle and
Secondary Classrooms 2nd Edition
https://ebookluna.com/product/ebook-pdf-developing-content-area-
literacy-40-strategies-for-middle-and-secondary-classrooms-2nd-edition/
https://ebookluna.com/download/business-communication-developing-leaders-
for-a-networked-world-ebook-pdf/
https://ebookluna.com/product/ebook-pdf-patterns-of-world-history-combined-
volume-2nd-edition/
c. Typechecking for React Applications
i. PropTypes
ii. Flow
iii. TypeScript
d. Test-Driven Development
i. TDD and Learning
e. Incorporating Jest
Copyright © 2020 Alex Banks and Eve Porcello. All rights reserved.
The views expressed in this work are those of the authors, and do not
represent the publisher’s views. While the publisher and the authors
have used good faith efforts to ensure that the information and
instructions contained in this work are accurate, the publisher and the
authors disclaim all responsibility for errors or omissions, including
without limitation responsibility for damages resulting from the use of
or reliance on this work. Use of the information and instructions
contained in this work is at your own risk. If any code samples or other
technology this work contains or describes is subject to open source
licenses or the intellectual property rights of others, it is your
responsibility to ensure that your use thereof complies with such
licenses and/or rights.
978-1-492-05172-5
[LSI]
Preface
This book is for developers who want to learn the React library while
learning the latest techniques currently emerging in the JavaScript
language. This is an exciting time to be a JavaScript developer. The
ecosystem is exploding with new tools, syntax, and best practices that
promise to solve many of our development problems. Our aim with this
book is to organize these techniques so you can get to work with React
right away. We’ll get into state management, React Router, testing, and
server rendering, so we promise not to introduce only the basics and
then throw you to the wolves.
This book does not assume any knowledge of React at all. We’ll
introduce all of React’s basics from scratch. Similarly, we won’t
assume that you’ve worked with the latest JavaScript syntax. This will
be introduced in Chapter 2 as a foundation for the rest of the chapters.
Along the way, check out the GitHub repository. All of the examples
are there and will allow you to practice hands-on.
Italic
Indicates new terms, URLs, email addresses, filenames, and file
extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to
program elements such as variable or function names, databases,
data types, environment variables, statements, and keywords.
TIP
This element signifies a tip or suggestion.
NOTE
This element signifies a general note.
WARNING
This element indicates a warning or caution.
Using Code Examples
Supplemental material (code examples, exercises, etc.) is available for
download at https://github.com/moonhighway/learning-react.
This book is here to help you get your job done. In general, if example
code is offered with this book, you may use it in your programs and
documentation. You do not need to contact us for permission unless
you’re reproducing a significant portion of the code. For example,
writing a program that uses several chunks of code from this book does
not require permission. Selling or distributing examples from O’Reilly
books does require permission. Answering a question by citing this
book and quoting example code does not require permission.
Incorporating a significant amount of example code from this book into
your product’s documentation does require permission.
If you feel your use of code examples falls outside fair use or the
permission given above, feel free to contact us at
[email protected].
How to Contact Us
Please address comments and questions concerning this book to the
publisher:
Sebastopol, CA 95472
707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and
any additional information. You can access this page at
https://oreil.ly/learningReact_2e.
For news and information about our books and courses, visit
http://oreilly.com.
Acknowledgments
Our journey with React wouldn’t have started without some good old-
fashioned luck. We used YUI when we created the training materials
for the full-stack JavaScript program we taught internally at Yahoo.
Then in August 2014, development on YUI ended. We had to change
all our course files, but to what? What were we supposed to use on the
front-end now? The answer: React. We didn’t fall in love with React
immediately; it took us a couple hours to get hooked. It looked like
React could potentially change everything. We got in early and got
really lucky.
We appreciate the help of Angela Rufino and Jennifer Pollock for all
the support in developing this second edition. We also want to
acknowledge Ally MacDonald for all her editing help in the first
edition. We’re grateful to our tech reviewers, Scott Iwako, Adam
Rackis, Brian Sletten, Max Firtman, and Chetan Karande.
There’s also no way this book could have existed without Sharon
Adams and Marilyn Messineo. They conspired to purchase Alex’s first
computer, a Tandy TRS 80 Color Computer. It also wouldn’t have
made it to book form without the love, support, and encouragement of
Jim and Lorri Porcello and Mike and Sharon Adams.
When React was first released, there was a lot of conversation around
whether it was good, and there were many skeptics. It was new, and the
new can often be upsetting.
To respond to these critiques, Pete Hunt from the React team wrote an
article called “Why React?” that recommended that you “give it
[React] five minutes.” He wanted to encourage people to work with
React first before thinking that the team’s approach was too wild.
Yes, React is a small library that doesn’t come with everything you
might need out of the box to build your application. Give it five
minutes.
Yes, in React, you write code that looks like HTML right in your
JavaScript code. And yes, those tags require preprocessing to run in a
browser. And you’ll probably need a build tool like webpack for that.
Give it five minutes.
A Strong Foundation
Whether you’re brand new to React or looking to this text to learn
some of the latest features, we want this book to serve as a strong
foundation for all your future work with the library. The goal of this
book is to avoid confusion in the learning process by putting things in a
sequence: a learning roadmap.
Before digging into React, it’s important to know JavaScript. Not all of
JavaScript, not every pattern, but having a comfort with arrays, objects,
and functions before jumping into this book will be useful.
In the next chapter, we’ll look at newer JavaScript syntax to get you
acquainted with the latest JavaScript features, especially those that are
frequently used with React. Then we’ll give an introduction to
functional JavaScript so you can understand the paradigm that gave
birth to React. A nice side effect of working with React is that it can
make you a stronger JavaScript developer by promoting patterns that
are readable, reusable, and testable. Sort of like a gentle, helpful
brainwashing.
From there, we’ll cover foundational React knowledge to understand
how to build out a user interface with components. Then we’ll learn to
compose these components and add logic with props and state. We’ll
cover React Hooks, which allow us to reuse stateful logic between
components.
Once the basics are in place, we’ll build a new application that allows
users to add, edit, and delete colors. We’ll learn how Hooks and
Suspense can help us with data fetching. Throughout the construction
of that app, we’ll introduce a variety of tools from the broader React
ecosystem that are used to handle common concerns like routing,
testing, and server-side rendering.
Harry ran out of the room, slamming the door behind him, but Mr.
Douglass only laughed. Perhaps he would have slammed it, too, if
he’d been in the boy’s place.
“Well?” said Philip, looking up from the Xenophon he was
translating.
“Thanks be to Christopher Columbus!” said Harry, with a jig-step.
“Has he done anything new?” Philip asked, looking over his
spectacles.
“I guess not,” said Harry, “but we’re going to the Fair.”
“How can we?” Philip asked.
Harry threw the cable despatch down upon the table, and turned
to get his hat. Philip read the telegram, carefully wiped his glasses,
rose, put the Xenophon into its place upon his book-shelves, and
said:
“Xenophon will have to attend to his own parasangs for a while.”
“You pack up for me, and I’ll see to the railroad-tickets,” said
Harry. “I have just about time to catch the train for New York.”
That was a hard and busy day for all three of the party. Perhaps
Harry’s share was the easiest, for, by showing his father’s despatch
to Mr. Farwell, he had everything made easy for him. Still, even
influence might not have secured them places except for the aid of
chance. It happened that a prominent man had, at the last moment,
to give up a section in the Wagner sleeper, and this was turned over
to Harry. So, late in the afternoon the boy came back with what he
called “three gilt-edged accordion-pleated tickets.”
Meanwhile Mr. Douglass and Philip had put into three traveling-
bags as much as six would hold, and the party went to bed early to
get a good rest before the long journey.
A WILDERNESS OF IRON.—BUILDING MACHINERY HALL.
“Didn’t you hear the waiter say dinner was ready?” said Mr.
Douglass.
“No,” said Philip; “but I knew it ought to be, if they care for the
feelings of their passengers. Where is the dining-car?”
“At the end of the train,” said Mr. Douglass. “Come, we’ll walk
through.”
So, in single file (“like cannibals on the trail of a missionary,” Harry
said), they passed from car to car. The cars were connected by
vestibules—collapsible passageways, folding like an accordion—and
it was not necessary to go outside at all. The train was an unbroken
hallway.
“It is much like a long, narrow New York flat,” said Philip. “People
who live in flats must feel perfectly at home when they travel in
these cars.”
They found the dining-car very pretty and comfortable. Along one
side were tables where two could sit, face to face. On the opposite
side of the aisle the tables accommodated four. The boys and their
tutor took one of the larger tables. The bill of fare was that of a well-
appointed hotel or restaurant,—soup, fish, entrées, joint, and
dessert,—and it was difficult to realize that they were eating while
covering many miles an hour; in fact, the only circumstance that was
a reminder of the journeying was a slight rim around the edge of the
table to keep the dishes from traveling too.
MAKING “STAFF.”
“It is strange,” said Mr. Douglass, “how people have learned to eat
dishes in a certain order, such as you see on a bill of fare. Probably
this order of eating is the result of tens of millions of experiments,
and therefore the best way.”
THE “COURT OF HONOR” AS IT LOOKED IN JUNE, 1892.
“The best for us,” said Philip; “but how about the Chinese?”
Mr. Douglass had to confess himself the objection well taken.
“I believe the Chinese were created to be the exceptions to all
rules,” he said.
The dining-car had an easy, swaying motion that was very
pleasant, and altogether the dinner was a most welcome change
from the ordinary routine of a railway journey.
As the boys walked back to their own section, Philip noticed a little
clock set into the woodwork at one end of the smoking-car. He was
surprised to see that it had two hour-hands, one red and one black.
He pointed it out to Mr. Douglass, who told him that the clock
indicated both New York and Chicago times—which differ by an hour,
one following what is called “Eastern,” the other “Central” time.
By the time they were again settled in their places it was dark
outside; and, as Philip poetically said, they seemed to be “boring a
hole through a big dark.” One of the colored porters looked curiously
at Philip, as if he had overheard this remark without understanding
its poetical bearing.
“He thinks you are a Western desperado!” said Harry, with a grin.
“Boys,” said Mr. Douglass, “the porters will soon make up the
beds, and I want you to see how ingeniously everything is
arranged.”
Here is what the porter did:
He stood straddling on two seats, turned a handle in the top of a
panel, and pulled down the upper berth. It moved on hinges, and
was supported after the manner of a book-shelf by two chains that
ran on spring pulleys.
Then he fastened two strong wire ropes from the upper to the
lower berths.
“What’s that for?” asked Harry.
“To prevent passengers from being smashed flat by the shutting
up of the berth,” Philip answered, after a moment’s puzzling over the
question.
“You can have the upper berth, Philip,” said Harry, impressively.
“It’s better ventilated than the lower, they say; but I don’t mind
that.”
Meanwhile the porter took from the upper berth two pieces of
mahogany, cut to almost fill the space between the tops of the seats
and the side roofs of the car. The edges were grooved, and slid
along upon and closely fitted the top of the seat and a molding on
the roof. These side-pieces were next fastened by a brass bolt
pushed up from the end of the seat-back.
Then the bed-clothing (kept by day in the lower seats and behind
the upper panel) was spread on the upper berth, and the mattress
of the lower berth was made up from the seat-cushions, supported
upon short slats set from seat to seat.
While the beds were being made, the boys were amused to see
some ladies laughing at the man’s method of getting the clothes and
pillows into place. A woman seems to coax the bed into shape, but a
man bullies it into submission.
“They think it’s funny to see him make a bed,” said Harry, in an
undertone; “but if they were to try to throw a stone, or bait a fish-
hook, I guess the darky would have a right to smile some too.”
To finish his work, the porter hung a thick pair of curtains on
hooks along a horizontal pole, and then affixed a long plush strip to
which were fastened large gilt figures four inches high—the number
of the section.
“It would be fun to change the numbers around,” remarked Harry,
pensively. “Then nobody would know who he was when he got up.
But perhaps it would make a boy unpopular if he was caught at it.”
Mr. Douglass admitted that it might.
As the porter made up their own section, Harry
pulled out his sketch-book and made a little
picture of him.
“It’s hard times on the railroad now,” he
remarked, as he finished the sketch. “See how
short they have to make the porters’ jackets! But
it must save starch!”
“YO’ SECTION
READY, SAH!”
The boys had wondered how the people would
get to bed, but there seemed no difficulty about it.
As for our boys, who had the upper berth, one by one they took off
their shoes, coats and vests, etc., and then climbed behind the
curtains, where they put their pajamas over their underclothes.
After they were in bed, they talked but little, for they were tired.
“This rocking makes me drowsy,” Philip said; “it’s like a cradle.”
“Yes,” Harry answered, as the car lurched a little—“a cradle rocked
by a mother with the St. Vitus’s dance!”
While going to sleep, the boys were puzzled to account for the
strange noises made by the train. At times it seemed to have run
over a china-shop, and at other times the train rumbled hoarsely, as
if it were running over the top of an enormous bass-drum.
Soon the great train was transporting two boys who were fast
asleep in Section No. 12; they woke fitfully during the night, but only
vaguely remembered where they were, until the cold light of
morning was reflected from the top of the car.
Dressing was more difficult than going to bed, but by a
combination of patience and gymnastics Harry and Philip were soon
able to take places in the line that led to the wash-room. Thence,
later, they came forth ready for breakfast (for which they had to “line
up” again), and another all-day ride.
THE OPENING OF THE FAIR, MAY 1, 1893.—THE PRESIDENT OF THE UNITED STATES SPEAKING.
The train had stopped there for five minutes, and they had
climbed down near the rapids to a point where there was an
excellent view of “the great cataract”—so young Phinney called it.
He gave the boys some pictures showing the falls, and indeed there
was a picture of the falls upon the side of the breakfast bill of fare.
During the forenoon the train was passing through Canada—the
boys’ impression of that country being a succession of flat fields,
ragged woods, sheep, swine, and a few pretty, long-tailed ponies
grazing upon browning turf. Philip said that it was like “the
Adirondacks spread flat by a giantess’s rolling-pin.”
At Windsor the train, separated into sections, was run upon a
ferry-boat (upon which one small room was marked “U. S.
Customs”) and carried over to Detroit. Here Mr. Douglass made the
boys laugh by suddenly jumping back from the window. He had
been startled by a large round brush that was poked against the
window from outside to dust it.
From Detroit the train ran through Michigan—mainly through a flat
country of rich farming land. Philip, who had never been West, was
much surprised at the uninterrupted stretches of level ground. Mr.
Douglass asked him what he thought of the region. Philip adjusted
his glasses and replied slowly: “Well, it’s fine for the farmers, but it is
no place for speaking William Tell’s piece about ‘Ye crags and peaks,
I’m with you once again!’”
“You must not forget, though,” said Mr. Douglass, “that it is the
rich farming lands that really underlie America’s prosperity. When
you see the Fair, you will understand better what a rich nation we
are; but without our great wheat-lands we should, like England, be
dependent upon commerce for our very existence.”
The boys were much less talkative as the train neared Chicago.
They were somewhat tired, and were also thinking of the amount of
walking and sight-seeing that was before them.
All at once, at about half-past five, New York time (for the
travelers had not yet changed their watches to an hour earlier), Mr.
Douglass pointed out of the right-hand forward window. Both boys
looked. There, in the distance, rose above the city houses a gilded
dome, and from the opposite car-window they saw just afterward a
spider-web structure.
“I know it!” Philip sang out; “that’s the Administration Building. But
what is the other?”
“The Ferris Wheel,” answered Harry.
“Yes,” said the tutor, “we are going to leave the car not far from
the Plaisance gate.”
“Sixtieth street next!” cried the brakeman.
“Come, we get out here. It’s nearest the grounds, and I have been
told it is wise to lodge as near as possible.”
When the cars stopped, the party descended upon a platform with
“rails to the right of them, rails to the left of them,” and trains and
crowds in all directions. Mr. Douglass led the way out into the
huddled settlement of apartment-houses, hotels, and lodgings that
has sprung into existence around Jackson Park, the Fair Grounds.
Then began their search for rooms. At first it seemed
discouraging; neatness outside was not always a sign of what to
expect inside. They labored up-stairs and down again several times.
At one attractive private house they entered, expecting quiet,
homelike rooms. In the tiny parlor they found five cots set “cheek by
jowl” as close as they could be jammed. They smiled at this, but
found the rest of the rooms as fully utilized. Mr. Douglass made
some objection, and was told by the self-possessed landlady that
“some very fine gentlemen thought her fifty-cent beds were very
elegant.” At another house they were passing, a boy who couldn’t
have been over five years old rushed out like a little Indian on the
warpath, crying, “Hi! You lookin’ fer rooms?” Amused at the little
fellow’s enterprise, our travelers followed him, the boy going forward
on his sturdy little legs, and crying, “Hi, there, Mama! Here’s
roomers! I got you some roomers!”
But unfortunately the boy proved more attractive
than the rooms. After a long walk, but without going
far from the Fair Grounds, they took rooms at a very
good hotel. The price was high, perhaps, but
reasonable considering the advantages and the
demand for lodgings. They took two rooms, one with a “HI, THERE, MAMA!
double bed for the boys, the other a single room for HERE’S ROOMERS!”
the tutor.
Gladly they dropped the satchels that had made their muscles
ache, and after leaving the keys of their rooms with the hotel clerk,
they set forth for their first visit to the Fair. In order that guests
should not forget to leave their keys, each was inserted at right
angles into a nickel-plated strip of metal far too long to go
comfortably into the pocket even of an absent-minded German
professor.
“One advantage of being in a hotel,” said Mr. Douglass, as they
walked toward the entrance of the grounds, “is the fact that on
rainy, disagreeable days we can get meals there if we choose. It is
not always pleasant to have to hunt breakfast through the rain. But
usually we shall dine where we happen to be in the grounds; there
are restaurants of all sorts near the exhibits, from a lunch-counter
up.”
Along the sidewalk that led from their hotel to the entrance were
dining-rooms, street-peddlers’ counters, peddlers with trays—all
meant as inducements to leave money in the great Western
metropolis. One thing the boys found very amusing was an Italian
bootblack’s stand surrounded on three sides by a blue mosquito-
netting.
“If it had been on all sides,” said Harry, “I could have understood
it, because it might be a fly-discourager. But now I think it must be
only a way of attracting attention.”
They had arrived, luckily, on a “fête night.” Though tired and
hungry, they all agreed that it would never do not to take advantage
of so excellent a chance to secure a favorable first impression. So
they bought tickets at a little wooden booth, and, entering a turnstile
one by one, were at last in the great White City.
HERE WE ARE!
ADMINISTRATION BUILDING.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookluna.com