mini-redux icon indicating copy to clipboard operation
mini-redux copied to clipboard

a small implementation of redux to better understand its design patterns

#mini-redux Small implementation of redux to better understand it's design patterns

const createStore = require('./mini-redux').createStore;

function AppReducer(state, action) {
    switch (action.type) {
        case 'inc':
            return state + 1
        default:
            return state
    }
}

var intialState = 0;

var store = createStore(intialState, AppReducer);

var unsubscribe = store.subscribe((state) => {
    console.log(state);
})

store.dispatch({ type: 'init'}) // => 0
store.dispatch({ type: 'foo'}) // => 0
store.dispatch({ type: 'inc'}) // => 1
store.dispatch({ type: 'inc'}) // => 2
store.dispatch({ type: 'bar'}) // => 2

unsubscribe()

License

MIT