What is redux and what does it serve for?

Redux provides the platform where you can write JavaScript apps that perform consistently in server, client, and native environments. There are several benefits to using Redux, prime among which is that bugs and errors can be detected easily. Also, Redux codes are extremely well organized, which makes it easy for anyone to get started with the same with the least of hassles. That is not all as the re-rendering process is also extremely easy as well.

Another inherent benefit of Redux is its size. It is just 2KB, which means it won’t be adding to the asset size of your application. Also, with Redux, your application state is maintained in a store. Any component will then be able to access any state that it requires from this very store. Redux also allows for cross-browser compatibility, which means apps built for one browser can also be accessible from just about any browser.

Also, Redux allows you to manage your app in one place, which ensures changes made to the app are more predictable and are easy to trace as well. That said, some argue that all of the benefits lead to what can be referred to as the addition of the boilerplate code. That said, sound architecture management can make things more efficient.

Another unique feature of Redux is that it plays host to more than 30 types of fields and validation options. With that many options at their disposal, developers have a powerful yet flexible framework with Redux. Essentially, there are three components to the way Redux works – Actions, Store, and Reducers. You can also read more about Redux.

Actions

Here Actions can be described as events and acts as the sole method of sending data from the application to the Redux store. The data again can be the result of some user interaction, API calls, as well as some data submitted in the form.

Basically, you use the store.dispatch() method to send actions. Also, those being JavaScript objects, there should be a type property that points out the action type that needs to be carried out. There has to be a payload as well which contains info which is what the action needs to act.

Here is an example of an action to be executed during login.

{

  type: “LOGIN”,

  payload: {

    username: “foo”,

    password: “bar”

  }

}

Reducers

Reducers, on the other hand are pure functions that take as input the current state of an application, performs an action on the same and produces as output a new state. Such states get stored as objects and are indicators of how the state of an application is likely to change when an action is sent to the store.

Here is an example of the manner Reducers work in Redux

onst LoginComponent = (state = initialState, action) => {

    switch (action.type) {

 

      // This reducer handles any action with type “LOGIN”

      case “LOGIN”:

          return state.map(user => {

              if (user.username !== action.username) {

                  return user;

              }

 

              if (user.password == action.password) {

                  return {

                      …user,

                      login_status: “LOGGED IN”

                  }

              }

          });

default:

          return state;

      }

};

Store

The Store is where the application state is held, with the general norm being that a single store is a recommendation for any Redux application. You will be able to access the stored state and update the state, besides registering or unregistering listeners using helper methods.

Here is how you create a store for the login apps as discussed before.

const store = createStore(LoginComponent);

On the whole, Redux provides a method to design apps in Javascript that will remain just as relevant in server, client, and native environments. That apart, Redux also ensures debugging is a completely hassle-free operation.

About the author

Allen Parker

Allen Parker

Allen is a qualified writer and a blogger, who loves to dabble with and write about technology. While focusing on and writing on tech topics, his varied skills and experience enables him to write on any topic related to tech which may interest him. You can contact him at allen@pc-tablet.com.