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); this.store = this.props.store; } handleVoteAngular = () => { this.store.dispatch(voteAngular()); } handleVoteReact = () => { this.store.dispatch(voteReact()); } handleVoteVueJs = () => { this.store.dispatch(voteVueJs()); } 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); this.store = this.props.store; } votesAngularInPercent() { if (this.store.getState().angular) { return (this.store.getState().angular / (this.store.getState().angular + this.store.getState().react + this.store.getState().vuejs)) * 100; } else { return 0; } } votesReactInPercent() { if (this.store.getState().react) { return (this.store.getState().react / (this.store.getState().angular + this.store.getState().react + this.store.getState().vuejs)) * 100; } else { return 0; } } votesVueJsInPercent() { if (this.store.getState().vuejs) { return (this.store.getState().vuejs / (this.store.getState().angular + this.store.getState().react + this.store.getState().vuejs)) * 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


Posted

in

by

Recent Post

  • Transforming HR with AI Assistants: The Comprehensive Guide

    The role of Human Resources (HR) is critical for the smooth functioning of any organization, from handling administrative tasks to shaping workplace culture and driving strategic decisions. However, traditional methods often fall short of meeting the demands of a modern, dynamic workforce. This is where our Human Resource AI assistants enter —a game-changing tool that […]

  • How Conversational AI Chatbots Improve Conversion Rates in E-Commerce?

    The digital shopping experience has evolved, with Conversational AI Chatbots revolutionizing customer interactions in e-commerce. These AI-powered systems offer personalized, real-time communication with customers, streamlining the buying process and increasing conversion rates. But how do Conversational AI Chatbots improve e-commerce conversion rates, and what are the real benefits for customers? In this blog, we’ll break […]

  • 12 Essential SaaS Metrics to Track Business Growth

    In the dynamic landscape of Software as a Service (SaaS), the ability to leverage data effectively is paramount for long-term success. As SaaS businesses grow, tracking the right SaaS metrics becomes essential for understanding performance, optimizing strategies, and fostering sustainable growth. This comprehensive guide explores 12 essential SaaS metrics that every SaaS business should track […]

  • Bagging vs Boosting: Understanding the Key Differences in Ensemble Learning

    In modern machine learning, achieving accurate predictions is critical for various applications. Two powerful ensemble learning techniques that help enhance model performance are Bagging and Boosting. These methods aim to combine multiple weak learners to build a stronger, more accurate model. However, they differ significantly in their approaches. In this comprehensive guide, we will dive […]

  • What Is Synthetic Data? Benefits, Techniques & Applications in AI & ML

    In today’s data-driven era, information is the cornerstone of technological advancement and business innovation. However, real-world data often presents challenges—such as scarcity, sensitivity, and high costs—especially when it comes to specific or restricted datasets. Synthetic data offers a transformative solution, providing businesses and researchers with a way to generate realistic and usable data without the […]

  • Federated vs Centralized Learning: The Battle for Privacy, Efficiency, and Scalability in AI

    The ever-expanding field of Artificial Intelligence (AI) and Machine Learning (ML) relies heavily on data to train models. Traditionally, this data is centralized, aggregated, and processed in one location. However, with the emergence of privacy concerns, the need for decentralized systems has grown significantly. This is where Federated Learning (FL) steps in as a compelling […]

Click to Copy