Redux is a state management library that helps you write applications that behave consistently and are easy to test. It's most commonly used with React for building user interfaces. It allows you to centralize the state of your app in a single store, and use actions and reducers to update the state in a predictable way. This article will explore how to create your own redux-like library from scratch in plain old JavaScript.
Note
The Redux-like library we're going to build will not have all the features of Redux and I won't recommend anyone to use this in a production environment. The goal of building this library is to help you understand how Redux works under the hood.
And now that we're on the same page, let's get started!
Core Redux Concepts
The basic idea behind Redux is that you have a centralized object that stores all the information of your app called state which can predictably update the state. To accomplish this, Redux has the following basic structure:
State
A state is a plain JavaScript object that represents the state of the application at a specific point in time. The state is managed by the store and can be accessed by any component in the application via the store. The state is read-only and the only way to change the state is by dispatching an action, which is a plain JavaScript object that represents an intention to change the state.
Store
A store is an object that holds the application's state tree. There is only a single store in a Redux application. A state is a plain JavaScript object that represents the state of the application at a specific point in time.
Action
An action is a plain JavaScript object that represents an intention to change the state. Actions are the only way to get data into the store. You send them to the store using the dispatch method.
Reducer
A reducer is a pure function that takes the previous state and an action as arguments and returns a new state. It describes how an action transforms the state into the next state.
Building our Redux
If you've worked with Redux in the past, you must be aware of the createStore method that creates the store. We'll start by creating our own createStore method which will have three methods, dispatch, subscribe, and getState.
export const createStore = (reducer, initialState) => {
let state = initialState; // setting our initial state
const listeners = []; // array of functions that'll hold the subscribe callbacks
function getState() {
// will return the current state
}
function dispatch(action) {
// will dispatch an action
}
function subscribe(fn) {
// will hold the listeners
}
return {
getState,
dispatch,
subscribe,
};
};
As you can see we've created a barebones createStore function that accepts two parameters the reducer function and the initialState and returns an object that consists of three methods getState, dispatch, and subscribe.
We've also created two variables inside this function called state and listeners. The state variable as the name suggests will hold the state of our Redux store and the listeners array will hold the callback functions which we'll pass to subscribe method. As soon as the createStore method is triggered we're assigning the initial state we're passing as the second parameter to the state variable.

Join the conversation! Your thoughts help the community grow.