LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub

Wire in Redux

In the previous tutorial, we have hooked up the Jest Test Runner and watch the file change to rerun all test cases. In this tutorial, we are going to wire in Redux to manage the state of the application.

Install Redux

First we need to add dependencies.

cd ~/networknt/light-portal/view
yarn add redux react-redux redux-thunk redux-logger

If you are using npm, then you can run

npm install --save redux react-redux redux-thunk redux-logger

After it is done, you should see the following dependencies in your package.json

  "dependencies": {
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-redux": "^6.0.0",
    "react-scripts": "2.1.2",
    "redux": "^4.0.1",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0"
  },

As the entire React ecosystem is changing very fast, the versions of each component might be different for you.

Files Structure

With Redux in the picture, we need to re-organize our project structure to make it easier to add new components later.

Let’s create the following folders in the src.

  • components
  • reducers
  • actions

Home Component

Let’s create the first component Home.js in components folder as our landing page.

import React, {Component} from 'react';

class Home extends Component {
    render() {
        return (
            <div>
                Home
            </div>
        )
    }
}

export default Home;

Reducers

We are planning to load menu for each host from our backend serivce so that each host can customized which applications can be accessed through menus. So when application is started, we should have a state that contains a list of menu items. Let’s create a reducer named menu.js as following.

export default function(state = [], action) {
    switch (action.type) {
        default:
            return state;
    }
}

As you can see, the function takes a state with an empty list as default and an action for parameters. As we don’t have any action yet, the default in switch block just return the current state.

Now, let’s create an index.js in the reducers folder as Root Reducer that can combine all other reducers together.

import { combineReducers } from 'redux';
import menuReducer from './menu';

export default combineReducers({
    menu: menuReducer
});

For now, we only have one reducer and more will be added later.

Provider

Now, let’s update the index.js in the src folder to wire in the Proivider tag. Open the file from src folder and add the following imports.

import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import rootReducer from './reducers';
import thunk from 'redux-thunk';
import logger from 'redux-logger';

Add the following const.

const store = createStore(rootReducer, applyMiddleware(thunk, logger));

And update the render to.

ReactDOM.render(
    <Provider store={store}>
      <App />
    </Provider>,
    document.getElementById('root')
);

Actions

In the actions folder, we are going to create a types.js and an index.js

types.js

export const LOAD_MENU = 'load_menu';

index.js

import { LOAD_MENU } from './types';

export function loadMenu(host) {
    return {
        type: LOAD_MENU,
        payload: host
    }
}

Now, double check the application still running without any error from the console tab of developers tool.

At this stage, we have wired in Redux to our application. We still need to update the components later to connect them with Redux store for states and actions.

Let’s check in the code to branch ui-wire-in-redux for this tutorial and in the next tutorial, we are going to add material-ui to our application.

git checkout -b ui-wire-in-redux
git add .
git commit -m "ui view wire in redux"
git push origin ui-wire-in-redux
  • About Light
    • Overview
    • Testimonials
    • What is Light
    • Features
    • Principles
    • Benefits
    • Roadmap
    • Community
    • Articles
    • Videos
    • License
    • Why Light Platform
  • Getting Started
    • Get Started Overview
    • Environment
    • Light Codegen Tool
    • Light Rest 4j
    • Light Tram 4j
    • Light Graphql 4j
    • Light Hybrid 4j
    • Light Eventuate 4j
    • Light Oauth2
    • Light Portal Service
    • Light Proxy Server
    • Light Router Server
    • Light Config Server
    • Light Saga 4j
    • Light Session 4j
    • Webserver
    • Websocket
    • Spring Boot Servlet
  • Architecture
    • Architecture Overview
    • API Category
    • API Gateway
    • Architecture Patterns
    • CQRS
    • Eco System
    • Event Sourcing
    • Fail Fast vs Fail Slow
    • Integration Patterns
    • JavaEE declining
    • Key Distribution
    • Microservices Architecture
    • Microservices Monitoring
    • Microservices Security
    • Microservices Traceability
    • Modular Monolith
    • Platform Ecosystem
    • Plugin Architecture
    • Scalability and Performance
    • Serverless
    • Service Collaboration
    • Service Mesh
    • SOA
    • Spring is bloated
    • Stages of API Adoption
    • Transaction Management
    • Microservices Cross-cutting Concerns Options
    • Service Mesh Plus
    • Service Discovery
  • Design
    • Design Overview
    • Design First vs Code First
    • Desgin Pattern
    • Service Evolution
    • Consumer Contract and Consumer Driven Contract
    • Handling Partial Failure
    • Idempotency
    • Server Life Cycle
    • Environment Segregation
    • Database
    • Decomposition Patterns
    • Http2
    • Test Driven
    • Multi-Tenancy
    • Why check token expiration
    • WebServices to Microservices
  • Cross-Cutting Concerns
    • Concerns Overview
  • API Styles
    • Light-4j for absolute performance
    • Style Overview
    • Distributed session on IMDG
    • Hybrid Serverless Modularized Monolithic
    • Kafka - Event Sourcing and CQRS
    • REST - Representational state transfer
    • Web Server with Light
    • Websocket with Light
    • Spring Boot Integration
    • Single Page Application
    • GraphQL - A query language for your API
    • Light IBM MQ
    • Light AWS Lambda
    • Chaos Monkey
  • Infrastructure Services
    • Service Overview
    • Light Proxy
    • Light Mesh
    • Light Router
    • Light Portal
    • Messaging Infrastructure
    • Centralized Logging
    • COVID-19
    • Light OAuth2
    • Metrics and Alerts
    • Config Server
    • Tokenization
    • Light Controller
  • Tool Chain
    • Tool Chain Overview
  • Utility Library
  • Service Consumer
    • Service Consumer
  • Development
    • Development Overview
  • Deployment
    • Deployment Overview
    • Frontend Backend
    • Linux Service
    • Windows Service
    • Install Eventuate on Windows
    • Secure API
    • Client vs light-router
    • Memory Limit
    • Deploy to Kubernetes
  • Benchmark
    • Benchmark Overview
  • Tutorial
    • Tutorial Overview
  • Troubleshooting
    • Troubleshoot
  • FAQ
    • FAQ Overview
  • Milestones
  • Contribute
    • Contribute to Light
    • Development
    • Documentation
    • Example
    • Tutorial
“Wire in Redux” was last updated: July 5, 2019: fixes two typos of wire-in-redux in portal view tutorial (82ce732)
Improve this page
  • News
  • Docs
  • Community
  • Reddit
  • GitHub
  • About Light
  • Getting Started
  • Architecture
  • Design
  • Cross-Cutting Concerns
  • API Styles
  • Infrastructure Services
  • Tool Chain
  • Utility Library
  • Service Consumer
  • Development
  • Deployment
  • Benchmark
  • Tutorial
  • Troubleshooting
  • FAQ
  • Milestones
  • Contribute