





















































There's this great new library called redux that has been making rounds recently for all of the right reasons. But, why are developers so crazy about redux, and why should you consider using redux in your next application? This post will explain what makes redux a good choice as we create a small application using redux and its principles.
Redux is based on three main principles; they are as follows:
The standard format of a reducer is as follows:
const myReducer = (state = defaultValue, action) => {
/*
perform some calculations based on action and old state.
newState !== state
*/
return newState;
};
Using this format, let’s write a reducer for a simple counter:
const counter = (state = 0, action) => {
const { type } = action;
switch (type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state + 2;
default:
return state;
}
};
So far, there has been no mention of redux, only of reducers. We now need redux as a glue to bridge our business logic (the counter reducer) to the store and the application state.
In order to make our application, we will use npm and ES6 modules. You can bootstrap a project easily using a yeoman generator like this one:
Install redux and react using the following:
npm install --save redux
Create the counter store:
import { createStore } from 'redux';
const store = createStore(counter);
In our html file, we will add a simple interface for our counter:
<html>
<head>
<title>My App</title>
</head>
<body>
<div id="counter-display">
</div>
<button id="counter-increment"> + </button>
<button id="counter-decrement"> - </button>
<script src="b/https://www.packtpub.com/qa-be/learning/tech-guides/undle.min.js"></script>
</body>
</html>
Next, let’s create a render method and subscribe our store to it such that it is called every time the state of the store is changed:
const counterDisplay = document.getElementById('counter-display');
const render = () => {
counterDisplay.innerHTML = store.getState();
};
store.subscribe(render);
render();
We also call the render method once in the beggining to render the app initially.
Now, we will add event listeners to the increment and decrement buttons to dispatch events every time they are clicked:
const incrementButton = document.getElementById('counter-increment');
const decrementButton = document.getElementById('counter-decrement');
incrementButton.addEventListener('click', ()=>{
store.dispatch({type : 'INCREMENT'});
});
decrementButton.addEventListener('click', ()=>{
store.dispatch({type : 'DECREMENT'});
});
Now we have a fully functioning counter. The data flow in/out counter is as follows:
The source code for this application can be found here, and the working example can be seen here.
Redux is so great because of the many developer tools that it makes available. This one is written by the creator of redux himself. A few of the reasons you should consider incorporationg developer tools into your development are as follows:
For small scale applications, redux devolopment tools provide an easy and convenient way to debug and inspect your application, and for larger applications, I would go so far as to say that they are required.
Soham Kamani is a Full Stack web developer and electronics hobbyist. He is especially interested in JavaScript, Python, and IOT. HisTwitter handle is @sohamkamani, and he can also be found here.