Skip to content

Instantly share code, notes, and snippets.

@kostasx
Forked from bloodyowl/redux-light-example.js
Created January 11, 2023 08:32
Show Gist options
  • Select an option

  • Save kostasx/166888f629def9b80f208f9efe558a80 to your computer and use it in GitHub Desktop.

Select an option

Save kostasx/166888f629def9b80f208f9efe558a80 to your computer and use it in GitHub Desktop.

Revisions

  1. @bloodyowl bloodyowl created this gist Feb 29, 2016.
    15 changes: 15 additions & 0 deletions redux-light-example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    const store = createStore((state = { counter: 0 }, action) => {
    switch(action.type) {
    case "INCREMENT":
    return { counter: state.counter + 1 }
    case "DECREMENT":
    return { counter: state.counter - 1 }
    default:
    return state
    }
    })

    store.dispatch({ type: "INCREMENT" })
    store.dispatch({ type: "INCREMENT" })

    console.log(store.getState())
    14 changes: 14 additions & 0 deletions redux-light.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    const createStore = (reducer, state = reducer(undefined, { type: "@@INIT" })) => {
    const subscribers = new Set()
    return {
    dispatch: (action) => {
    state = reducer(state, action)
    subscribers.forEach(func => func())
    },
    subscribe: (func) => {
    subscribers.add(func)
    return () => subscribers.delete(func)
    },
    getState: () => state
    }
    }