DEV Community

Cover image for Create redux in 28 lines of code
Shanoy Sinclair
Shanoy Sinclair

Posted on • Updated on

Create redux in 28 lines of code

Its such an eye opener when you learn to create the tools you are using and get a glimpse of how some of its functionality works under the hood.

function createStore(reducer, initialState) {
    let state = initialState;

    function getState() {
        return state;

    function dispatch(action) {
        state = reducer(state, action);

    return { dispatch, getState };

function reducer(state = [], action) {
    switch (action.type) {
        case "add":
            return [...state, action.payload];
            return state;

const store = createStore(reducer);
store.dispatch({ type: "add", payload: "learn to code" });
store.dispatch({ type: "add", payload: "react" });
//[ 'learn to code', 'react' ]
Enter fullscreen mode Exit fullscreen mode

inspired by: typeofnandev

Top comments (2)

bias profile image
Tobias Nickel

actually, your implementation only 10 lines. I think there is enough space left for .subscribe().

shanoysinc profile image
Shanoy Sinclair

Yes your right lol I will be added subscribe next and possible try create the wrappers for react