Skip to main content

How to use Reducers effectively ( React Redux )


Reducers are part of Redux (which is a predictable state container for JS apps). Redux is used mostly for application state management in JS frameworks / libraries. Redux maintains the state of an entire application in a single immutable state tree (object) which cannot be changed directly. To change something, a new object must be created (using actions and reducers).

So what are reducers anyway ?

Reducers are used in response to an action which is sent to the store (a single immutable store). Reducers specify how the application’s state changes while action only describe what happens. You can think of reducers like an state changer, Let’s take an example of simple application where you want display list of food dishes using React js with Redux. Now, this list will usually have 3 actions: 

i) DISHES_LOADING : which indicates dishes are been loading.

 ii) DISHES_FAILED : which indicates the application failed to fetch the dishes details.

 iii) ADD_DISHES : which indicates adding new dishes to a list.

Fig.1. ActionTypes.jsx file
Fig.2. Dishes Reducer

Here you can see we don’t mutate the state , we simply create a return the the new copies of the object. The default state returns the previous state.

But what if I had more than one reducer for my app?

You might need more than reducer for your app and you also want those reducers to have their results combined into a single object ( rather than different copies of the object ). Let’s take another reducer for comments:

Again, there will be three actions for comment: 

i) ADD_COMMENT: which will indicate adding a new comment.

ii) COMMENT_FAILED: which will indicate comment the user was trying to put failed.

iii) ADD_COMMENTS: which will add all the comments from the store.

Fig.3. Comment Reducer

Now, we need to combine these two reducers in order to create only a single new object when there is a state change. To do that use combineReducers() which generate a function that calls your reducers with the slices of the state selected according to their keys, and combines their results into a single object again.

Fig.4. Combining Reducer

This way you can easily create a reducer for your actions for the app you are creating. Always note down the actions of your component/components before you start coding, this way you need not worry about adding new actions later on.


Thank you for reading :)

Comments

Popular posts from this blog

How to use Chess com API using Python

  How to use Chess com API using Python Chess is an amazing strategy-based game and if you have been following the recent boom of online chess then welcome to the chess club. Online chess is amazing since it allows you to play with a random stranger at your level or stockfish (computer). There are many popular online chess websites like lichess.org, chess.com, playchess.com, and newly created kasparovchess.com. Today we will be seeing how to use chess.com API for getting players' stats. You can create software and get affiliates from them (check out the link below), so share it with them if you are planning to create something. Before you start make sure you have the following things: Pre-requirements Postman Anaconda or mini conda or Python idle Any text editor of your choice Pretty good? Now let’s download the JSON file that chess com developers have already made for us from here and then you import it to the Postman. This just helps you with prewritten get methods so that ...

Create your own YouTube video and playlist downloader using QT designer in Python

Create your own YouTube video and playlist downloader using QT designer in Python The code for this software is available on my GitHub, the link is given below. Create your own YouTube video and playlist downloader using QT designer in Python This is the thing we are trying to make today for this article The code for this software is available on my GitHub, the link is given below. If you haven’t checked out my previous article on the same topic but not the GUI version, please check it out. 3 ways to download YouTube playlists at once using Python Have you guys ever wanted to download your entire youtube playlist? rahulbhatt1899.medium.com So before starting to code let's check if you have all the requirements beforehand: Pre-requirements : Qt designer Anaconda or mini conda or python 3.7 compiler Any text editor of your choice Libraries to download: Pytube Ffmpy PyQt5 This would be sectioned into the following structure: Creating the UI in Qt designer Converting UI file int...

Authentication Using Passport JS

Authentication is big and most complicated part of an application, but it is also the  preponderance  part of any web app/software in general. Passport JS Passport JS is a widely popular authentication module for  Node js.  The sole purpose is to authenticate requests and is based on the idea of pluggable authentication strategies ( including local strategy, there are more than 500 strategies currently  available  ). When using third party application Your app never receives your password  thus freeing the developer from the burden of security related to handling and storing passwords. The Passport authentication and its strategy will include protection against attacks like “man in the middle” and other vectors an attacker might exploit. We are going to use Facebook strategy, for now, to install Passport and Facebook strategy type in the following command: Next, we are going to write the authentication code, and we’ll be creating a different module cal...