Coder Social home page Coder Social logo

logisticinfotech / easiest-demo-redux-reactjs Goto Github PK

View Code? Open in Web Editor NEW
3.0 4.0 1.0 87 KB

Easiest demo of redux in reacjs with little ui state management

Home Page: https://www.logisticinfotech.com/blog/easiest-demo-to-learn-redux-in-reactjs-with-code-example/

HTML 6.24% CSS 2.77% JavaScript 90.99%
redux react-redux reactjs tutorial demo-app demo react-redux-demo html

easiest-demo-redux-reactjs's Introduction

Easiest React Redux Demo with little UI state managment like tab, input, select, checkbox, radio

Purpose of this demo is to understand redux easily. You can find step by step guide here to understand this demo properly.

React Redux configuration

First install react-redux

npm install react-redux --save

Now, you need to define provider and store in index.js

import { Provider } from 'react-redux';
import { store } from './reducers';

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

Now, we need to create two files :

  1. action/index.js
export const tabClicked = (activeTab) => ({
    type: 'tabClicked',
    activeTab: activeTab
});
  1. reducers/index.js
import { combineReducers, createStore } from 'redux';

// Initial State
const initialState = {
    activeTab: 1
}

// reducers part
export const actionReducer = (state = initialState, action) => {
    switch (action.type) {
        case 'tabClicked':
            return {
                ...state,
                activeTab: action.activeTab
            }
        default:
            return state;
    }
};


export const reducers = combineReducers({
    actionReducer
});

// store part
export const store = createStore(reducers);

How to use in component

import { tabClicked } from './../../actions';

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

function mapStateToProps(state) {
    return {
        activeTab: state.actionReducer.activeTab,
    }
}

const mapDispatchToProps = {
    tabClicked
};

const TabContainer = connect(
    mapStateToProps,
    mapDispatchToProps
)(TabComponent);

We can use props and event in html. You just need to put html in redner function in component.

<a className={"nav-item nav-link" + (this.props.activeTab === 1 ? ' active' : '')} id="nav-tab1-tab" data-toggle="tab" href="#nav-tab1" role="tab" aria-controls="nav-tab1" aria-selected="true" onClick={() => this.props.tabClicked(1)}>Tab 1</a>

Development server

Run npm start for a dev server. Navigate to http://localhost:3000/. The app will automatically reload if you change any of the source files.

Further help

Checkout full blog here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.