ReactJs Redux

How to Use Redux in Your ReactJS App in Just 10 Minutes?

In this post, we will learn the principles behind Redux and how to use Redux in a ReactJS-based application. Let’s get started!

If you have decided to use Redux as your state management library, now you actually have done the hard part. Deciding which library to use. Everything else is very easy and fast. Follow along to create a to-do app with Redux in just 5 minutes.

The Idea Behind Redux

You have mainly three components: store, reducer, and actions.

As per the Redux docs:

Store holds the whole state [tree] of your application. The only way to change the state inside it is to dispatch an action on it.

Actions are payloads of information that send data from your application to your store. They are the onlysource of information for the store.

We will define two actions, createItem and deleteItem.

Reducers specify how the application’s state changes in response to actions sent to the store. Remember that actions only describe what happened, but don’t describe how the application’s state changes.

We will create one reducer just like a table in a SQL DB, but you can create more if you need to.


Step 1

Create a new React app using CRA (create-react-app).

create-react-app todo-app

Step 2

Install dependencies.

npm install lodash @material-ui/core @material-ui/icons react-redux redux redux-logger

Step 3

If you want to learn an efficient way to store your files, use the ducks file structure. But, for simplicity, we will be using a simple file structure.

Create a new folder inside src named “modules.”

Create three files inside the folder: action.js, reducer.js, store.js

Actions are simple objects with one mandatory property type. You can dispatch actions from your component to send data to the state store.


// types of action
const Types = {
// actions
const createItem = task => ({
  type: Types.CREATE_ITEM,
  payload: task
const deleteItem = id => ({
  type: Types.DELETE_ITEM,
  payload: id
export default {

Ideally, you should create another file for your action types.

import ACTIONS from "./action";
import _ from "lodash";
const defaultState = {
  items: []
const todoReducer = (state = defaultState, action) => {
  switch (action.type) {
    case ACTIONS.Types.CREATE_ITEM: {
      let item = action.payload;
      let newItem = { id: state.items.length + 1, description: item };
      let newState = _.cloneDeep(state);
      return newState;
    case ACTIONS.Types.DELETE_ITEM: {
      let newState = _.cloneDeep(state);
      let index = _.findIndex(newState.items, { id: action.payload });
      newState.items.splice(index, 1);
      return newState;
      return state;
export default todoReducer;


import { createStore, applyMiddleware } from "redux";
// Logger with default options
import logger from "redux-logger";
import reducer from "./reducer";
export default function configureStore(initialState) {
  const store = createStore(reducer, initialState, applyMiddleware(logger));
  return store;

Step 4

Create a new todo.js file in a new folder inside src named “pages.” This will contain our todo list component. This example is focused on Redux so we won’t try to create a component/containers. We will have our entire app in this file only.

Create the UI with a form and a list to show the tasks.


import React, { Component } from "react";
import {
} from "@material-ui/core";
import DeleteIcon from "@material-ui/icons/Delete";
const styles = theme => ({
  root: {
    flexGrow: 1,
    maxWidth: 752
  demo: {
    backgroundColor: theme.palette.background.paper
  title: {
    margin: `${theme.spacing.unit * 4}px 0 ${theme.spacing.unit * 2}px`
class ToDO extends Component {
  state = {};
  generate = () => {
    return => (
      <ListItem key={}>
        <ListItemText primary={item.description} />
            <DeleteIcon />
  handleSubmit = event => {
    // console.log(this.state.item);
    this.setState({ item: "" });
    if (this.state.item !== "") {
      // add the task to store
  handleDelete = event => {
    //delete the task from the store
  handleChange = event => {
  render() {
    const { classes } = this.props;
    return (
          <form noValidate autoComplete="off" onSubmit={this.handleSubmit}>
                label="New Task"
          <Grid item container justify="space-evenly" alignItems="center">
            <div className={classes.demo}>
              <List dense={false}>{this.generate()}</List>
export default withStyles(styles)(ToDO);

Step 5

It’s time to connect our todo component to the Redux store.

Open your app.js file and add the Redux provider so all of the child properties can access the store.

import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";
import ToDO from "./pages/todo";
import { Provider as ReduxProvider } from "react-redux";
import configureStore from "./modules/store";
const reduxStore = configureStore(window.REDUX_INITIAL_DATA);
class App extends Component {
  render() {
    return (
      <ReduxProvider store={reduxStore}>
        <div className="App">
          <header className="App-header">
            <img src={logo} className="App-logo" alt="logo" />
            <h1 className="App-title">ToDo Redux app</h1>
          <ToDO />
export default App;

Inside your todo.js file, connect it with the store using the connect component from react-redux. Add these at the end of the file in todo.js.

Now, the items from store will be available in the component’s props. Also, the other two functions (actions) will be available in props.

import ACTIONS from "../modules/action";
import { connect } from "react-redux";
const mapStateToProps = state => ({
  items: state.items
const mapDispatchToProps = dispatch => ({
  createItem: item => dispatch(ACTIONS.createItem(item)),
  deleteItem: id => dispatch(ACTIONS.deleteItem(id))
export default connect(

Lastly, call createItem and deleteItem inside of handleSubmit and handleDelete, respectively.

handleSubmit = event => {
  // console.log(this.state.item);
  this.setState({ item: "" });
  if (this.state.item !== "") {
    // add the item to the store
handleDelete = event => {
  // delete the item from the store


And that’s it! Did this work for you? Please leave any questions and comments below!

Thank you for reading! If you found this article helpful, please like it!

Rahul Sharma

Rahul Sharma is a Founder of Logical IDEA and Bee Flirty. He is a Professional WP Developers, UX Designer, and Digital Marketing Expert. Previously, He Worked as a Digital Marketing Head for An eCommerce Startup. He Graduated with honors from Lovely Professional University with a Dual Degree in Information Technology.

Leave a Reply

Your email address will not be published. Required fields are marked *