Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
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
Daniel Bugl Daniel Bugl
Author Profile Icon Daniel Bugl
Daniel Bugl
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

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

Tying state and actions together

After defining the application state and actions, we still need a way to apply actions to change the application state. In Redux, the state is updated through special functions called reducers. Reducers contain the state changing logic of our application.

newState = reducer(state, action)

A reducer function takes the current state object and an action object as arguments. The reducer parses the action object, specifically, the action.type. Depending on the action type, the reducer function either returns a new state, or it simply returns the current state (if the action type is not handled in this reducer).

To write a function, we first have to think of the function signature (the head of the function). A reducer function takes the current state and an action argument. For the state argument, we set a default value, which is what the initial state is going to be.

In our example application, the initial state is an empty array of posts, so we can define the reducer function, as follows:

function postsReducer (state = [], action) { 

Now, we will need to parse the action object. The most common way to handle actions in Redux is using a switch statement on action.type. That way, we can have separate cases for all the different action types that the reducer function is going to take care of:

  switch (action.type) {   

In the switch statement, we handle the CREATE_POST action we defined earlier using Array.concat to add the new post object to the state (an array of posts):

    case 'CREATE_POST': 
return state.concat([{ user: action.user, text: action.text }])

For all other action types, we simply return the current state:

    default: 
return state

}
}
Please note that the default branch is very important. If you do not return the current state for unhandled action types, your state will become undefined.
You have been reading a chapter from
Learning Redux
Published in: Aug 2017
Publisher: Packt
ISBN-13: 9781786462398
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 $19.99/month. Cancel anytime
Banner background image