Four most IMP(important) Rules of Reducers!🔥🧨🎇
Tags: react, redux, reducers, javascript
Published: 2020-01-06 18:46:58

---
title: Four most IMP(important) Rules of Reducers!🔥🧨🎇
published: true
description: 
tags: react, redux, reducers, javascript
---

Hi, coding people 🙋🏻‍♂️✋🏻

As you might know, my name is [Atul](atulprajapati.in/) and I write articles about coding, freelancing and most of Web. Now in this article, I am going to memories you of the four most important rules of reducer + explanation which is a child of Redux.

So let's dive in 🥽🏊🏻‍♂️

**1** Must return any value besides 'undefined' 🚫

🤚That means your reducer must return something anyway. your reducer should not be written undefined in any case. It can be written 

**Any Number** 123

![Four rules fo reducers by Atul Prajapati](https://thepracticaldev.s3.amazonaws.com/i/j3oesncf2wm2zoozttpi.png)

**Any string** abc

![Four rules fo reducers by Atul Prajapati](https://thepracticaldev.s3.amazonaws.com/i/zwheuiabw0uhcwzu45ei.png)

 **[] Empty array**

![Four rules fo reducers by Atul Prajapati](https://thepracticaldev.s3.amazonaws.com/i/n1dlzawsnoj33cij021w.png)

 **{} Empty object**

![Four rules fo reducers by Atul Prajapati](https://thepracticaldev.s3.amazonaws.com/i/mzgz3kuf5nertgmzva69.png)

**And null**

![Four rules fo reducers by Atul Prajapati](https://thepracticaldev.s3.amazonaws.com/i/qn2zo1mhls1c4wh7m398.png)

**but not undefined**

![Four rules fo reducers by Atul Prajapati](https://thepracticaldev.s3.amazonaws.com/i/2pu7kghjc6qk7v3qeemi.png)

if you using undefined you are most likely going to see this beautiful error 😁🤦‍♂️ 
![Four rules fo reducers by Atul Prajapati](https://thepracticaldev.s3.amazonaws.com/i/c83hk87n5zqo89nw4m9p.png)

**NOTE: It does not react, a redux rule that is how javascript functions work.**

As you can see an **error screenshot** that shows your reducer can't return undefined during initialization 🚴🏻‍♀️ but that's not it. your reducer can't return undefined in never ever when reducer initialized first time or any time in future when the action has been dispatched.

___

**2** Reducer produces 'state', or data to be used inside of your app using only the previous state and the action.

🥵🤒 it's totally confusing don't sweat it lets break it so you can understand too.

So whenever your React.js application starts with redux your reducer before produce or catches new data from the state it uses old state or data to add your new data with it so that you can get your data.

![Rules of reducer by Atul Prajapati](https://thepracticaldev.s3.amazonaws.com/i/qje901cs5ezw3ygr9vd8.png)

🔥🔥🔥In this diagram, you see the reducer position when it first time going to call.

When you first start your Redux application each reducer initialize with two arguments

**A.undefined**
**B.Action #1**

The reducer uses these two✌️ arguments to produce new state values.

if you say Atul 🥴 you said in the first rule that reducer "Must return any value besides 'undefined'" and now reducer gets undefined argument so my answer is

**Reducer automatically gets undefined value and javascript does not allow it that's why we set it to value either null or anything empty.**

The things get really interesting when reducer get's called on the second time.

![Rules of reducer by Atul Prajapati](https://media.giphy.com/media/Zd5r9yumBioNH3db6P/giphy.gif)

So this is the position of our reducer when it runs the second time.

![Rules of reducer by Atul Prajapati](https://thepracticaldev.s3.amazonaws.com/i/sx9yndandw37b3sgg3r9.png)

As you see here when our reducer gets called first time it uses two arguments first is null/undefined and second is action and produced state V1

Now when reducer called the second time it uses our state v1 and our new action(Action #2) to produce new state(State V2)


___

**3** Must not return reach 'out of itself' to decide what value to return (reducers are pure) 🚕🏂🏻

In this rule author trying to say that reducer nothing doing other things like API calls, document.write function and anything else. It just uses two arguments A.previous state B.action doing some competition or calculation to decide what to show next. That's all the works of reducer.


___

**4** Must not mutate its input 'state' argument 🤷🏽‍♂️🤸🏽‍♂️

Okay, that's not the worst thing in the world.

This rule is trying to say that we can't mutate state in the reducer.

actually in react it's recommended that don't use 

array/obg.push()

array/obg.pop()

methods to react.

React has there inbuild methods to do this activity. 🔥🔥🔥


**Thank's 😍💝 for reading. If you are interested to learn more like this make sure follow me So that when I post my article you will get notified **


📸/[atulcodex](https://www.instagram.com/atulcodex/)  👨🏻‍💻/[Atul Prajapati](https://www.linkedin.com/in/atulcodex/)