Implementing Redux Architecture using React-Hooks

Building a Todo app with React Hooks

akshay patil
Apr 22, 2019 · 5 min read

React, one of the best libraries to build front-end web applications. A lot has changed over few years with React library. The latest version of React i.e., 16.8 introduces a new concept of Hooks. This, gives a better way to write components in React. React was always down seen because it wasn’t a full blown framework and it lacked state management. With some of the features of hooks like useContext(), createContext(), useState() and useReducer() the state management problem of React is finally solved. No need to install a third party library to achieve and manage state.

If you have worked with Redux(A state management) library then learning to manage the state with React hooks will be very simple. For this, we need to go back to the basics of React-Redux architecture.

React-Redux Architecture

We need Three main components as usual to setup our boiler plate for React-Redux app.

  1. Store: Single source of truth.
  2. Action functions: functions which dispatch actions with type and payload.
  3. Reducers: A function which catches the action and manipulates the Store depending on the action.

Let’s create an application to better understand the concept. We will be creating a todo application.

Todo App with React-Hooks:

Start the project using the create-react-app CLI tool.

If you get stuck anywhere in between. You can use the Github link to verify your code.


$ create-react-app todo-app

Now, let’s create a file Store.js in src/ folder. Create a store using the createContext() hook in Store.js:

import React from "react";

Now, let’s start creating the reducer for our store. As usual each reducer will need an initial state, similar to our redux reducers. And a function which will accept initial state and action as input. Lets, create that in our Store.js:

const initialState = {
todos: [];

Let’s create a store provider in our Store.js which will act as our Provider in Redux. So that we will be able to server Store all over the app.

export function StoreProvider(props) {
const [state, dispatch] = React.useReducer(reducer, initialState)
const value = {state, dispatch}
return <Store.Provider value={value}>{props.children} </Store.Provider>

import store provider from Store.js into your index.js:

import { StoreProvider } from "./Store.js";

Apply the provider around the <App/> within the ReactDOM.render of index.js:

ReactDOM.render(<StoreProvider><App /></StoreProvider>, document.getElementById('root'));

Now that we have all the setup, all we have to do now is to build the components.

Let’s see how we can make the store available to the components and how to dispatch actions to our reducer.

First, make the store available to component. Delete everything in App.js and write the below code:

import { Store } from "./Store";

In the above code you can see how we have mapped our state and dispatch to be used within our component. Thanks to useContext() ! and we have one local state to hold all our component local data.

Next, we have two action functions putData() and doneTodo() which will dispatch actions with type and payload. Very similar to our React-Redux

All thats left now is a return() content to display all of our content. Add a return right below the action functions:

import { Store } from "./Store";

Add some styling to the App according to your will, I have given a sample CSS below for App.css:

.App {
text-align: center;

Go ahead and fire up the local server to see how our app looks right now.

$ npm start
todo app


React has evolved into a very powerful library in 2019. With all the features of a framework, React is a library used to create static web pages. It has a lot of advantages over a full blown web framework and I am sure there are a lot of features coming up soon which will make usage of React for developers even more easier.

Thats all folks, Thank you!

akshay patil

Written by

Full-Stack Developer

More From Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade