Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Learning Redux

You're reading from   Learning Redux Write maintainable, consistent, and easy-to-test web applications

Arrow left icon
Product type Paperback
Published in Aug 2017
Publisher Packt
ISBN-13 9781786462398
Length 374 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Bugl Bugl
Author Profile Icon Bugl
Bugl
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Title Page
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. Why Redux? FREE CHAPTER 2. Implementing the Elements of Redux 3. Combining Redux with React 4. Combining Redux with Angular 5. Debugging a Redux Application 6. Interfacing with APIs 7. User Authentication 8. Testing 9. Routing 10. Rendering on the Server 11. Solving Generic Problems with Higher-Order Functions 12. Extending the Redux Store via Middleware

Making functions pure


Before we learn how to make generic abstractions for our reducers, we will take a closer look at pure functions. We have already learned a bit about them in the previous chapters. Pure functions have no side effects; they always return the same output, given the same input. The following is what makes Redux so predictable:

  • Reducers take the current state and an action and return the new state
  • All reducers are pure functions
  • Given the same input, pure functions return the same output
  • As a result, given the same state and action, reducers will always return the same new state

When writing abstractions on top of our reducers, we need to make sure that they are also pure functions, otherwise we will lose the benefits of Redux and introduce unpredictable behavior.

Simple side effects

Let's take a look at an impure function. The following example has a side effect:

let i = 0
function counter () {
  return i++
}

In this case, the function is modifying a global variable, which causes...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images