Voting App Using React Redux

Redux provides an easy way to centralize the state of your application. There are three basic properties to know in Redux. Action, Store, Reducer. We will cover all these properties one by one.

The Redux Cycle

The redux cycle is composed of Action, Store and Reducers. Action is like a message that we send or dispatch to our central Redux Store. The Store is the central part and it saves the global app state. Reducers calculates the next state ie. they handles the main flow. If any change in state occurs, the reducer will check which action type is triggered and then reducer changes the state for that particular action type.

I have learned about the basic redux flow. I created a voting app which with the help of redux will do comparison between three technologies.

The Basic Directory Structure

In the above image, there is a src folder which contains the main flow of the application. It has sub-directories such as components, reducers and actions.

In actions folder → index.js

// Define action types as constants export const VOTE_REACT = 'VOTE_REACT'; export const VOTE_ANGULAR = 'VOTE_ANGULAR'; export const VOTE_VUEJS = 'VOTE_VUEJS'; // Action creators export const voteReact = () => { return { type: VOTE_REACT }; } export const voteAngular = () => { return { type: VOTE_ANGULAR }; } export const voteVueJs = () => { return { type: VOTE_VUEJS }; }
Code language: JavaScript (javascript)

The above index.js file contains the action type for three technologies – Angular, React and VueJs.

In reducers→ index.js

const initialState = { angular: 0, react: 0, vuejs: 0 } export default (state = initialState, action) => { switch (action.type) { case 'VOTE_ANGULAR': console.log('Vote Angular!'); return Object.assign({}, state, { angular: state.angular + 1 }); case 'VOTE_REACT': console.log('Vote React!'); return Object.assign({}, state, { react: state.react + 1 }); case 'VOTE_VUEJS': console.log('Vote VueJs'); return Object.assign({}, state, { vuejs: state.vuejs + 1 }); default: return state; } }
Code language: JavaScript (javascript)

In the above index.js file, the reducer takes 2 arguments (state, action). So depending upon the type of the action the reducer changes the state and returns it to the global store.

In src→ index.js

import React from 'react'; import ReactDOM from 'react-dom'; import { createStore } from 'redux'; import './index.css'; import App from './App'; import myApp from './reducers'; import Results from './components/results'; import registerServiceWorker from './registerServiceWorker'; // Create the Redux store for the application let store = createStore(myApp); function render() { ReactDOM.render( <App />, document.getElementById('root') ); registerServiceWorker(); } store.subscribe(render); render();
Code language: JavaScript (javascript)

This index.js is the file where you initialize the app and call ReactDom.

src→ App.js

import React, { Component } from 'react'; import { voteAngular, voteReact, voteVueJs } from './actions/index'; import './App.css'; class App extends Component { constructor(props) { super(props); =; } handleVoteAngular = () => {; } handleVoteReact = () => {; } handleVoteVueJs = () => {; } render() { return ( // Your JSX code goes here ); } } export default App;
Code language: JavaScript (javascript)

What is your favorite front-end framework in 2017

The above App.js is the main component of the react application.

Components→ results.js

import React, { Component } from 'react'; export default class Results extends Component { constructor(props) { super(props); =; } votesAngularInPercent() { if ( { return ( / ( + + * 100; } else { return 0; } } votesReactInPercent() { if ( { return ( / ( + + * 100; } else { return 0; } } votesVueJsInPercent() { if ( { return ( / ( + + * 100; } else { return 0; } } votesAngularInPercentStyle() { return { width: this.votesAngularInPercent() + "%", }; } votesReactInPercentStyle() { return { width: this.votesReactInPercent() + "%", }; } votesVuejsInPercentStyle() { return { width: this.votesVueJsInPercent() + "%", }; } render() { return ( <div> <span className="label label-danger"> Angular: {this.votesAngularInPercent().toFixed(2) + '%'}</span> <div className="progress progress-striped active"> <div className="progress-bar progress-bar-danger" style={this.votesAngularInPercentStyle()}></div> </div> <span className="label label-danger"> React: {this.votesReactInPercent().toFixed(2) + '%'}</span> <div className="progress progress-striped active"> <div className="progress-bar progress-bar-danger" style={this.votesReactInPercentStyle()}></div> </div> <span className="label label-danger"> VueJs: {this.votesVueJsInPercent().toFixed(2) + '%'}</span> <div className="progress progress-striped active"> <div className="progress-bar progress-bar-danger" style={this.votesVuejsInPercentStyle()}></div> </div> </div> ); } }
Code language: JavaScript (javascript)

The above results.js file contains the component called Result.

All the components of the application will come under the components folder.

The output of the above application




Recent Post

  • Mastering Software Testing Strategies: Your Guide

    Implementing best software testing strategies is a crucial part of software development, ensuring that digital products meet industry standards. Defined by the International Software Testing Qualification Board, it encompasses a range of activities, both static and dynamic, throughout the software’s lifecycle. As an essential component of the Software Development Life Cycle (SDLC), the Software Testing […]

  • Your Complete Guide to Securing Your Kubernetes Cluster: Essential Tips for Beginners

    In today’s digital landscape, Kubernetes has emerged as the go-to solution for managing applications at scale. However, amidst its power lies a crucial concern: security. Ensuring the safety of your Kubernetes cluster is paramount to protecting your applications, data, and infrastructure. This comprehensive guide will walk you through essential practices and tips to help you […]

  • How to use Cloudinary in Node.js to upload images?

    It’s simple to use Cloudinary to upload images in a Nodе.js application, which can improve your app’s imagе management capabilities. Wе’ll go over how to sеt up and incorporate Cloudinary in your Nodе.js application for imagе uploads and how to use Cloudinary nodejs in this blog article. What is Cloudinary? Cloudinary is a cloud-based media […]

  • Mobile App User Experience- 10 Biggest Mistakes First-Time Founders Make 

    With the mobile app industry hitting a whopping 206 Billion in value (as per Grand View Research), it is no wonder more entrepreneurs are jumping into the mobile app market with their innovative ideas and life-changing solutions. But for those taking their first plunge, it is crucial to understand that creating an app isn’t just […]

  • Building Draggable Components in React

    As user interaction becomes more prevalent in today’s applications, sometimes it’s nice to use draggable elements in your apps. Consider website builders like Trello or Wix. They have drag-and-drop features that make it easy and convenient for users. Hello everyone! In this article, I will show some simple ways to create draggable components in React […]

  • How to Fetch Data from an API with JavaScript?

    In web development, integrating data from external sources is a common task. Application Programming Interfaces (APIs) play an important function in changing data amongst systems. In this blog, we’ll explore the way to Get information from an API with the use of JavaScript, supplying step-by-step the resource of-step commands with complete code examples of how […]