Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Learning Redux
Learning Redux

Learning Redux: Write maintainable, consistent, and easy-to-test web applications

Arrow left icon
Profile Icon Bugl
Arrow right icon
Can$38.99 Can$55.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (3 Ratings)
eBook Aug 2017 374 pages 1st Edition
eBook
Can$38.99 Can$55.99
Paperback
Can$69.99
Subscription
Free Trial
Arrow left icon
Profile Icon Bugl
Arrow right icon
Can$38.99 Can$55.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (3 Ratings)
eBook Aug 2017 374 pages 1st Edition
eBook
Can$38.99 Can$55.99
Paperback
Can$69.99
Subscription
Free Trial
eBook
Can$38.99 Can$55.99
Paperback
Can$69.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Learning Redux

Implementing the Elements of Redux

After getting the first taste of Redux, we will now go into more detail on the elements that make up Redux:

  • Actions: JavaScript objects, which describe state changes in the application
  • Action creators: Functions that take some arguments and return an action
  • Reducers: Functions that take the current state and an action, and return the new state
  • Store: The heart of Redux, which actually stores and guards your application state; we can subscribe to state changes in the store and dispatch actions to it

In this chapter, we will discuss how to implement these elements and how to put them together. Furthermore, we will create our first small project using Redux. It will be a simple blog that lists posts and reacts to user input when we click on these posts.

The Redux cycle

An action is dispatched

It all starts with a call to store.dispatch(action), which means that some action happened (user action, request resolved, and so on). An action is a plain object that describes what happened:

{ type: 'CREATE_POST', user: 'dan', text: 'hello world' }
It might help to think of the action as a kind of a log entry that tells we what happened, for example, Post hello...

Running code examples

All examples in this book use the new JavaScript syntax ES6/ES2015. Newer browser and Node.js versions already support most of this syntax (at the time of this writing, import/export is not supported anywhere, except Babel). If we want to support older versions of browsers or Node.js, we will need to transpile your code to the older JavaScript version ES5 via a tool, such as Babel (https://babeljs.io/). In addition to transpiling, Babel also allows you to use JSX syntax (an extension of JavaScript used with React) and even the future JavaScript syntax, where the proposal is already finalized, but not implemented in any engines yet.

Setting up a new project

This section explains how to set up a complete...

Implementing actions and action creators

In the first chapter, we learned how important actions are in Redux. Only actions can change the state of your application.

Actions in Redux are valid if they have a type property. The rest of the object structure is totally up to you. If you prefer using a standard structure, check out https://github.com/acdlite/flux-standard-action.

Redux actions are simple JavaScript objects with a type property, which specifies the name of the action.

Let's define a simple action, in the src/index.js file:

const createPost = { type: 'CREATE_POST', user: 'dan', text: 'New post' }
console.log(createPost)

Now, save the file. Webpack should automatically refresh the page after modifying and saving a source file (such as src/index.js).

...

Implementing reducers

In the first chapter, we learned about reducers, functions that take the current state and an action and return the new state of the application. As you now know, actions only describe what happened. Reducers use that information to compute the new state.

To be able to figure out state changes, we should first think about the state of our application. We have already done this in the first chapter, and our state looks as follows:

{ 
posts: [
{ user: 'dan', category: 'hello', text: 'Hello World!' },
{ user: 'des', category: 'welcome', text: 'Welcome to the blog' }
],
filter: 'hello'
}

As you can see, we have two separate sub-states in the main state object: posts and filter. This is a good hint for making two reducers: a postsReducer to handle the posts sub-state, and a filterReducer...

The store – combining actions and reducers

By now, we might be wondering what exactly Redux is (besides a set of principles and some helper functions). Essentially, Redux brings actions and reducers together. It provides a store, which contains the application state and provides some functions to do the following:

  • Access the application state: store.getState()
  • Dispatch actions: store.dispatch(action)
  • Register listeners: store.subscribe(listener)

According to the official documentation of Redux, Redux is a predictable state container for JavaScript apps.

The state container (store) is the heart of Redux: It contains your application state and does not allow external changes. Redux only allows changes through actions, which get passed to pure reducer functions. These restrictions that Redux enforces are what makes your application state predictable.

...

Summary

In this chapter, we first learned how to set up a project that is ready for Redux and the new JavaScript ES2015 syntax. Then, we implemented the basic elements of Redux and learned how ES2015 syntax makes implementing them easier. Finally, you also learned how to put a full Redux project together, as well as subscribe to state changes and dispatch actions.

In the next chapter, we will discuss how we can render user interfaces with React and use it together with Redux. Afterwards, we will build a full application with Redux and React.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Write applications that behave consistently, run in different environments (client, server and native), and are easy to test
  • Take your web apps to the next level by combining the power of Redux with other frameworks such as React and Angular
  • Uncover the best practices and hidden features of Redux to build applications that are powerful, consistent, and maintainable

Description

The book starts with a short introduction to the principles and the ecosystem of Redux, then moves on to show how to implement the basic elements of Redux and put them together. Afterward, you are going to learn how to integrate Redux with other frameworks, such as React and Angular. Along the way, you are going to develop a blog application. To practice developing growing applications with Redux, we are going to start from nothing and keep adding features to our application throughout the book. You are going to learn how to integrate and use Redux DevTools to debug applications, and access external APIs with Redux. You are also going to get acquainted with writing tests for all elements of a Redux application. Furthermore, we are going to cover important concepts in web development, such as routing, user authentication, and communication with a backend server After explaining how to use Redux and how powerful its ecosystem can be, the book teaches you how to make your own abstractions on top of Redux, such as higher-order reducers and middleware. By the end of the book, you are going to be able to develop and maintain Redux applications with ease. In addition to learning about Redux, you are going be familiar with its ecosystem, and learn a lot about JavaScript itself, including best practices and patterns.

Who is this book for?

This book targets developers who are already fluent in JavaScript but want to extend their web development skills to develop and maintain bigger applications.

What you will learn

  • • Understand why and how Redux works
  • • Implement the basic elements of Redux
  • • Use Redux in combination with React/Angular to develop a web application
  • • Debug a Redux application
  • • Interface with external APIs with Redux
  • • Implement user authentication with Redux
  • • Write tests for all elements of a Redux application
  • • Implement simple and more advanced routing with Redux
  • • Learn about server-side rendering with Redux and React
  • • Create higher-order reducers for Redux
  • • Extend the Redux store via middleware

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 31, 2017
Length: 374 pages
Edition : 1st
Language : English
ISBN-13 : 9781786469533
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Aug 31, 2017
Length: 374 pages
Edition : 1st
Language : English
ISBN-13 : 9781786469533
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Can$6 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Can$6 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Can$ 201.97
Learning Redux
Can$69.99
React and React Native
Can$69.99
React Design Patterns and Best Practices
Can$61.99
Total Can$ 201.97 Stars icon

Table of Contents

12 Chapters
Why Redux? Chevron down icon Chevron up icon
Implementing the Elements of Redux Chevron down icon Chevron up icon
Combining Redux with React Chevron down icon Chevron up icon
Combining Redux with Angular Chevron down icon Chevron up icon
Debugging a Redux Application Chevron down icon Chevron up icon
Interfacing with APIs Chevron down icon Chevron up icon
User Authentication Chevron down icon Chevron up icon
Testing Chevron down icon Chevron up icon
Routing Chevron down icon Chevron up icon
Rendering on the Server Chevron down icon Chevron up icon
Solving Generic Problems with Higher-Order Functions Chevron down icon Chevron up icon
Extending the Redux Store via Middleware Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(3 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Just Some Guy Jun 01, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is THE BOOK YOU WANT to learn Redux. I just read it, and it answered all the questions I had about Redux from other sources. Filled with great, in depth examples, clear explanations, and just the right level of technical depth, this book should be on every React developer's shelf. As a bonus, it uses all the latest ES6 conventions, as well as delving into how to approach essential concerns like testing, authentication, server-side rendering, and more. I don't usually gush about tech books, but nobody has reviewed this book yet so I want to make my opinion clear. THIS BOOK IS A MUST READ if you're a serious developer and you want to work with Redux. Buy it. Read it. Thank me later. ;-)
Amazon Verified review Amazon
tmok2000 Jul 20, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a very well written book. The author is clearly very knowledgeable about the subject, as well as the use of the JavaScript/ECMAScript language. Not only he explains everything well, his code is beautifully written, too. I really appreciate that, as I always strive to write beautiful code myself. However, as anything in a fast moving technological world, the Webpack config part of the book is already outdated. I had to work a little harder to make it work with Webpack 4, but it's all good now. Also, Babel now has a new preset called "env", which the book doesn't use. It still advises to use the es2015 preset. These are minor issues. The book overall is very good, and I certainly learned a lot from it.
Amazon Verified review Amazon
Jose Garcia Jun 03, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book delivers in a big way! The author begins with the foundations of Redux and goes into practical details to explain the concepts that build Redux and the mindset behind it. By doing a simple project and scaling that project throughout the book, the reader gains deeper insight, best practices, and practical applications that can be applied to the reader's projects.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.