Book Image

Learning Redux

By : Bugl
Book Image

Learning Redux

By: Bugl

Overview of this book

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.
Table of Contents (13 chapters)

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.