Learning React Functional Web Development with React and Redux 1st Edition Alex Banks - The newest ebook version is ready, download now to explore
Learning React Functional Web Development with React and Redux 1st Edition Alex Banks - The newest ebook version is ready, download now to explore
com
https://ebookname.com/product/learning-react-functional-web-
development-with-react-and-redux-1st-edition-alex-banks/
OR CLICK HERE
DOWLOAD EBOOK
https://ebookname.com/product/learning-react-native-building-
native-mobile-apps-with-javascript-1-early-release-edition-
bonnie-eisenman/
https://ebookname.com/product/the-road-to-react-the-react-js-in-
javascript-book-2024-edition-robin-wieruch/
https://ebookname.com/product/learning-javascript-design-
patterns-a-javascript-and-react-developer-s-guide-2nd-edition-
adnan-osmani/
https://ebookname.com/product/painting-islam-as-the-new-enemy-
abdulhay-y-zalloum/
The Ritual Theatre of Theodoros Terzopoulos 1st Edition
Freddy Decreus
https://ebookname.com/product/the-ritual-theatre-of-theodoros-
terzopoulos-1st-edition-freddy-decreus/
https://ebookname.com/product/a-theory-of-language-and-mind-
ermanno-bencivenga/
https://ebookname.com/product/dialectical-behavior-therapy-for-
wellness-and-recovery-interventions-and-activities-for-diverse-
client-needs-1st-edition-andrew-bein/
https://ebookname.com/product/applied-mathematical-methods-in-
theoretical-physics-2ed-edition-masujima-m/
https://ebookname.com/product/jacques-lacan-and-education-a-
critical-introduction-1st-edition-donyell-l-roseboro/
Small Space Organizing 3rd Edition Kathryn Bechen
https://ebookname.com/product/small-space-organizing-3rd-edition-
kathryn-bechen/
Learning React
Functional Web Development
with React and Redux
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning React, the cover image, and
related trade dress are trademarks of O’Reilly Media, Inc.
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-491-95462-1
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Welcome to React. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Obstacles and Roadblocks 1
React Is a Library 2
New ECMAScript Syntax 2
Popularity of Functional JavaScript 2
JavaScript Tooling Fatigue 2
Why React Doesn’t Have to Be Hard to Learn 3
React’s Future 3
Keeping Up with the Changes 4
Working with the Files 4
File Repository 4
React Developer Tools 5
Installing Node.js 6
2. Emerging JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Declaring Variables in ES6 10
const 10
let 10
Template Strings 12
Default Parameters 13
Arrow Functions 14
Transpiling ES6 17
ES6 Objects and Arrays 19
Destructuring Assignment 19
Object Literal Enhancement 20
The Spread Operator 22
iii
Promises 24
Classes 25
ES6 Modules 27
CommonJS 28
4. Pure React. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Page Setup 59
The Virtual DOM 60
React Elements 62
ReactDOM 64
Children 65
Constructing Elements with Data 67
React Components 68
React.createClass 69
React.Component 72
Stateless Functional Components 73
DOM Rendering 74
Factories 77
iv | Table of Contents
Validating Props with createClass 110
Default Props 114
Custom Property Validation 115
ES6 Classes and Stateless Functional Components 116
Refs 119
Inverse Data Flow 121
Refs in Stateless Functional Components 123
React State Management 123
Introducing Component State 124
Initializing State from Properties 128
State Within the Component Tree 130
Color Organizer App Overview 130
Passing Properties Down the Component Tree 131
Passing Data Back Up the Component Tree 134
8. Redux. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
State 184
Actions 187
Action Payload Data 189
Reducers 190
The Color Reducer 193
The Colors Reducer 195
The Sort Reducer 197
Table of Contents | v
The Store 198
Subscribing to Stores 201
Saving to localStorage 202
Action Creators 203
Middleware 206
Applying Middleware to the Store 207
vi | Table of Contents
Universal Routing 310
Communicating with the Server 318
Completing Actions on the Server 318
Actions with Redux Thunks 321
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
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 Redux, React Router, and build tooling, 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 ES6
or any of the latest JavaScript syntax. This will be introduced in Chapter 2 as founda‐
tion for the rest of the chapters.
You’ll be better prepared for the contents of the book if you’re comfortable with
HTML, CSS, and JavaScript. It’s almost always best to be comfortable with these big
three before diving into a JavaScript library.
Along the way, check out the GitHub repository. All of the examples are there and
will allow you to practice with hands-on examples.
ix
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter‐
mined by context.
x | Preface
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].
O’Reilly Safari
Safari (formerly Safari Books Online) is a membership-based
training and reference platform for enterprise, government,
educators, and individuals.
Members have access to thousands of books, training videos, Learning Paths, interac‐
tive tutorials, and curated playlists from over 250 publishers, including O’Reilly
Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Profes‐
sional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press,
John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe
Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and
Course Technology, among others.
For more information, please visit http://oreilly.com/safari.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at http://bit.ly/learning-react-2e.
To comment or ask technical questions about this book, send email to bookques‐
[email protected].
For more information about our books, courses, conferences, and news, see our web‐
site at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Preface | xi
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 pro‐
gram that we taught internally at Yahoo. Then in August 2014, development on YUI
ended. We had to change all of 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 couple of hours to get hooked. It looked like React could
potentially change everything. We got in early and got really lucky.
This book would not have been possible without the support of Ally MacDonald who
helped us every step of the way and was monumentally patient with us through sev‐
eral library updates. We’re grateful to Melanie Yarbrough, Colleen Toporek, and
Rachel Head for their amazing attention to detail. Thanks to Sarah Ronau for proof‐
reading this book well before it was ready for human eyes and to Bonnie Eisenman
for her great advice and overall delightfulness. Thanks also to Stoyan Stefanov, who
was nice enough to provide a technical review even though he’s really busy building
cool stuff at Facebook.
There’s also no way this book could have existed without the 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, sup‐
port, and encouragement of Jim and Lorri Porcello, and Mike and Sharon Adams.
We’d also like to acknowledge Coffee Connexion in Tahoe City, California for giving
us the coffee we needed to finish this book, and its owner, Robin, who gave us the
timeless advice: “A book on programming? Sounds boring!”
xii | Preface
CHAPTER 1
Welcome to React
React is a popular library used to create user interfaces. It was built at Facebook to
address some of the challenges associated with large-scale, data-driven websites.
When React was released in 2013, the project was initially viewed with some skepti‐
cism because the conventions of React are quite unique.
In an attempt to not intimidate new users, the core React team wrote an article called
“Why React?” that recommended that you “Give It [React] Five Minutes.” They
wanted to encourage people to work with React first before thinking that their
approach was too crazy.
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. 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.
If you read that article—as we did—you may have been dazzled by the promise of a
new JavaScript library—a library that would solve all of our problems with the DOM;
a library that would always be easy to work with and would never hurt us.
Then the questions start to arise: how do I convert this JSX? How do I load data?
Where does the CSS go? What is declarative programming? Every path leads to more
questions about how to incorporate this library in your actual day to day work. Every
conversation introduces new terminology, new techniques, and more questions.
1
Other documents randomly have
different content
The Project Gutenberg eBook of Expedition to
Pluto
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: English
The great space captain smiled easily. "No need to beg my pardon at
all. At first glance one would think you had the right of it, but I just
happen to have gone into the matter a little deeper. You understand
the reasons behind this voyage? Well, suppose that after having
been away for two years we come back right on schedule, but
without a load of beryllium, without having found any trace of it.
What will happen? The League of Planets simply orders out another
expedition, better equipped, and we go down as having failed."
"But Captain! In two years there may not be enough light alloys left
on the three planets to build another ship as big as this! The service
to Mars will have to be stopped long before that. The lithium mines
there can't operate unless the water supply from Venus is
maintained."
"Well, what of it? Nobody likes to work in that Martian colony."
Adam caught his breath.
"But how are the atomic motors that do all the work going to
operate without the lithium from Mars?"
"And what of that, even? It would be temporary. Just a few months
or years till another expedition could be sent out. You take things
too seriously. What is there to prove that some other method of
armoring space-ships won't be found? Beryllium may not be
necessary after all."
"Perhaps you're right." Adam was still skeptical. "But is it likely, sir?
You've been on the space run so long you haven't kept up with
chemistry. The armor against meteorites now is so thick that any
metal but beryllium would double the weight of the vessel, and even
with these seven million horsepower Buvier-Manleys we couldn't
make the run from Venus to Mars. Why, sir, it would mean the end
of the lithium mines, it would mean the end of atomic power, and
we'd have to go back to the barbarism of the twentieth century,
when they ran everything by electricity from waterfalls!"
Captain McCausland raised an athletic hand. "Spare me that, Mister
Mate! I have heard it at approximately a hundred banquets before
starting out on this expedition. Yes, we carry the fate of the world
and all that. We have to find beryllium or else the Mars mines can't
be run and the atomic motors stop. I could sing it in my sleep. But
suppose we do take chances and get this ship wrecked. Won't the
world have to go back to 'barbarous' electric power after all? For my
part, I think some of those people in the twentieth century probably
had a good time."
Adam was silent. There was something in the Captain's reasoning,
he felt. Yet he, Adam Longworth, could not but feel that the issue
was a desperately serious one for every inhabitant of the three
worlds—Earth, Venus and Mars—belonging to the planetary league.
The entire known supply of beryllium, the precious light, strong
metal that was alone suitable for the armor of space ships, had been
exhausted. All that remained was in the hulls of the few dozen ships
carrying water from Venus to Mars, and from the arid deserts of
Mars, bringing to Earth the equally precious lithium which was the
only material with which atomic motors could be powered.
Every year, in spite of the best of care, one or two space ships would
be wrecked—caught in the sun's gravitational field, or lost through
some small error of navigation. Soon there would be no more space
ships; and no more could be built. Each of the outer planets had
been explored in turn—each but the last, the outermost and most
distant; Pluto. They were on their way there now; if they could not
make it—
"Very well, sir," he said aloud. "I see your point. Will you take over
the controls at the change of course?"
"I'll take over now. Report in two hours.... One more thing,
Longworth. You're young, damn young, to be first mate on this
expedition. You know you were a last-minute choice, because of an
accident to a much more experienced, and from what I've seen so
far, a much better man. Make the most of your chance, but don't
forget I'm captain here. I can't go into my reasons for everything I
do. That's all, Mister Mate."
"Hello, Earth! This is your radio gal, Paulette de Vries, speaking from
7-LOP, space-ship Goddard . Interplanetary time two-two-0-three, or
just three seconds behind schedule for entering the atmosphere of
Pluto. We're falling rapidly toward the planet. I can only see half of
it, filling the entire horizon. The color is almost exactly that of a
pearl in moonlight, white with blue lights and absolutely featureless.
Sunlight out here is indescribably weak. Our spectroscope, handled
by Professor Reuter, shows the atmosphere is high in fluorine, with
traces of argon, and outside that a thin belt, a very thin belt of
helium and hydrogen. I told you all that the other day. We have
accurate temperature readings now, folks, and what they say is 200
degrees below zero, which is plenty chilly. You could drive a nail with
a butter hammer at that temperature, folks, and it means we will
have to do our exploring by diving, since the whole surface of the
planet will be covered, perhaps miles deep, by liquid gases. Can't tell
till we get there. Once we do get in, however, these talks will
temporarily cease, folks. I'll be sorry, because I've enjoyed them,
and I've enjoyed hearing from all of you back on Earth, so many
million miles away. But I'll be back, and so will all the crew and its
heroic captain. You may remember—Stand by! We're in the
hydrogen layer now. It's misty, streaming past the ports, so I can
hardly see anything. I must sign off now. This is 7-LOP, space-ship
Goddard."
Adam Longworth crouched motionless. The muscles bulged along his
arms, shoulder and neck quivered with tension, and perspiration
stood in tickling beads on his skin. His eyes were fixed on the control
panel before him; on either side was a quartermaster at a set of
controls and behind the three stood Captain McCausland, calm and
watchful.
Adam's hand moved rapidly and a quivering needle stood still on a
dial.
"Three gravities insufficient."
The Captain's finger found a red button on the portable signal panel
that made a three-inch medallion on the left breast of his uniform.
Throughout the ship there was a flash of red lights; loudspeakers
echoed his "Stand by for five gravities."
The quartermasters flung long levers; the motors boomed, braking
the speed of the Goddard's fall toward the surface. Captain
McCausland slumped to the acceleration and recovered; the air-
speed indicator crept toward the bottom of the dial, and had almost
reached it, when a loudspeaker twanged nasally. "Visibility fifty feet,
liquid surface. Forty feet—going down—afloat, sir."
Adam killed the motor with a plunge of his finger and his ears rang
in the sudden stillness.
"Thank you, gentlemen," said the Captain. "Perfect landing, Mister
Longworth. Relieve the navigation watch and report to the chart-
room in ten minutes."
Adam saluted, said a few words to the quartermasters and went out
with them to the fo'castle. The men off watch were just unstrapping
themselves from their bunks. Jake Burchall stepped up.
"Do I take up the new watch, Mr. Adam?"
"Two hours from now. Look here, Jake, there's something I wish
you'd do."
"Yes sir."
Adam lowered his voice.
"Did you hear the report? Fluorine all through this planet. Our ports
are glass, and fluorine acts on glass. They're thick, of course, and
have layers of plastic, but it will wear them through eventually.
We've got to think of something to do about it, and I think I have
the answer. Remember compartment eighteen? She'll be flooded.
Suppose when you're posting that watch you get into a space suit
and slide in there. Don't go outside, but cut loose some of the mica
lining around the break. When you get it, you can make some mica
sheets for the helmet view-ports of your space suit. Then get into
compartment eighteen again, and try it out. I'm no chemical expert,
but mica should insulate the view-ports on the helmet, and if it'll do
that, it will insulate the view-ports of the whole ship. Report to me
privately. I don't want to make a fool of myself if Professor Reuter
already has some other scheme worked out."
Jake grinned in understanding.
"Yes sir. He-he-he. Hope he hasn't."
In the chart room, when Adam arrived, he found a small gathering.
Perkins, the chemists, was there; so was Professor Reuter, the
astronomical man, a couple of assistants, and Captain McCausland,
looking extremely grave, thoughtful, but ruffled.
"Well, Longworth," he said. "You're just in time to order out the
navigation watch and set the course back to Earth."
Adam was aghast. "Not really?"
"Ask these gentlemen." He indicated the scientists.
Professor Reuter cleared his throat, but it was Perkins who spoke.
"At least we cannot remain here. The fluorine here will gradually, but
certainly, cut through the glass in this ship."
Adam flushed. He burst out: "But you knew there was fluorine a
month ago! Didn't anyone—?"
Captain McCausland raised his hand. "Please."
Professor Reuter explained. "To tell the truth there was some
discussion at that time. I am afraid I must confess myself
considerably at fault. Dr. Perkins at that time urged that the
expedition return and install quartz ports on the Goddard. At that
time I judged the temperature would be about what it is, minus 200,
and at that figure fluorine would not be present in the liquid portion
of the atmosphere, but would exist as a gas, and therefore would
not make contact with our ports while diving."
"I warned you it would be in solution," remarked Dr. Perkins.
"Yes." Professor Reuter, a big man, with folds of fat hanging from his
cheeks, pursed his lips and blew through them. "At the time, I must
confess, I really must confess, that I failed to consider the fact that
the enveloping upper atmosphere of the planet would cause the
surface temperature to be lower than that in the atmosphere itself.
As a result, it is just cold enough to hold a certain amount of fluorine
in solution in this cold ocean—"
"And, in a nut-shell, we must turn back," said Adam. He appealed to
Captain McCausland. "Isn't there anything aboard ship with which
we could insulate the ports?" Scientists always made difficulties, he
thought; an old space captain like McCausland would not be so hard
to down.
"We can try putting divers in space suits with double-thickness glass
in the ports. That would last a couple of hours at all events. I don't
doubt but we could get volunteers in this crew."
Reuter blew through his thick lips again. "Dangerous. As scientific
head of the expedition I will permit nothing of the kind. Naturally,
Captain, we are under your orders, but if you take such a step it will
be without my authority."
McCausland made a gesture of hopelessness. "Have you any other
ideas to suggest, Mister Longworth?"
Was "Old Steel-Wall" giving up this easily? Adam's thoughts
wheeled, but he schooled himself to inquire mildly, "Have you
thought of trying mica windows, sir?"
"That would do it!" cried Dr. Perkins excitedly. "Fluorine doesn't
attack mica—at least at Earth temperatures. I don't know about
these sub-zero atmospheres, but it ought to work. Have we the
mica?"
"The ship is lined with it," remarked McCausland. "But we can't very
well take the ship apart."
"Compartment eighteen, sir!" Adam burst out.
"Try it by all means: Hurry though, for we'll have to shutter the
ship's ports within an hour or get out of here. I congratulate you,
Mister Longworth. That was well thought of."
Was there a touch of irony in the voice? As Adam saluted and
withdrew, he wondered. Nobody else appeared to have noticed it, to
have noticed that there was something in the Captain's voice that
didn't somehow sound quite right.
Three men were grouped around the air lock at eighteen, and they
looked up as the First Mate approached. "He's coming out now, sir,"
said one of them. "Been in already once, and Bjornsen fitted a mica
shield over one of his helmet ports. He's trying that and the straight
glass shield in comparison."
Adam nodded wordlessly, watching the lock handle. Presently it
turned and out stumbled a figure like a gnome, cased in hairy hoar-
frost. "Pretty cold out there," said one of the men, as with gloved
fingers he labored deftly at Burchall's helmet.
The little wizened face came out of it, grinning like a monkey's. "It
works, sir! I was in a good five minutes. Look here—the glass lens is
all pitted and scored, but the mica isn't touched. Something funny
out there in that ocean, though."
"What do you mean?"
"Something with legs, only they weren't exactly legs, either—"
"Careful with that space suit there, Jake. Material's brittle after that
cold."
Adam raised his voice. "Look here men. We have a job on our
hands. We must make and install port covers for every port on the
ship. You know, the regular collision covers—beryll-steel. Jake, go up
forward and get a dozen men, while you, Bjornsen, fix those mica
covers on a dozen space suit helmets. Make it snappy, for Heaven's
sake. We have just one hour to work in."
"Beg pardon, sir, but wouldn't it be easier to do it outside this
atmosphere?"
"Haven't the fuel. Hurry!"
The ship rang with orderly disorder, as man after man of the off-duty
watch reported, received the space suit with the new mica windows,
and passed out through the air-lock in compartment eighteen to join
the others who were adjusting protective collision-shutters over the
big ship's ports. The last man through, Adam embarked on an
inspection tour of the ship. Compartment 23 checked—all ports
shuttered; compartment 22—
A bell rang violently, and the loudspeaker system shouted: "First
Mate Longworth wanted in the Captain's cabin at once. First Mate
Longworth wanted—"
"First Mate Longworth reporting," Adam remarked into his chest
phone, and hurried along the corridor.
Captain McCausland was seated at his desk, drumming on it with his
fingers. "Mister Mate," he burst out, as Adam entered, "do you know
what time it is?"
"No, sir."
The Captain indicated the space-chronometer set in the wall. "Your
hour is up. Call your men in. We're leaving."
"But, sir, they're nearly finished—"
"Mister Mate, I have taken the trouble to explain my orders to you
once before. I'll do it once more, so there will be no possible
mistake. I'm responsible for the safety of this expedition and the
lives of the people aboard. In the present case it's my responsibility
to see that this cold fluorine ocean doesn't eat through the ports and
put everyone to a horrible death, corroding as it freezes them—
including Miss de Vries. Call in your men. That's an order!"
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.
ebookname.com