DEV Community

๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ

Posted on • Updated on

Four most IMP(important) Rules of Reducers!๐Ÿ”ฅ๐Ÿงจ๐ŸŽ‡

Hi, coding people ๐Ÿ™‹๐Ÿปโ€โ™‚๏ธโœ‹๐Ÿป

As you might know, my name is Atul 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

Any string abc

Four rules fo reducers by Atul Prajapati

[] Empty array

Four rules fo reducers by Atul Prajapati

{} Empty object

Four rules fo reducers by Atul Prajapati

And null

Four rules fo reducers by Atul Prajapati

but not undefined

Four rules fo reducers by Atul Prajapati

if you using undefined you are most likely going to see this beautiful error ๐Ÿ˜๐Ÿคฆโ€โ™‚๏ธ
Four rules fo reducers by Atul Prajapati

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

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ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

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

Rules of reducer by Atul Prajapati

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 ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป/Atul Prajapati

Top comments (2)

Collapse
 
iftikhar profile image
iftikhar hussain

very helpful

Collapse
 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ

Thanks share this post so our community can find any time in future ๐Ÿค˜โ˜๏ธโœŒ๏ธ