Created
October 10, 2015 09:24
-
-
Save matthewmueller/8ec5cfba57152d11fe1b to your computer and use it in GitHub Desktop.
redux reduxed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Debug = require('redux-debug') | |
var debug = require('debug') | |
var assign = require('object-assign') | |
var thunk = require('redux-thunk') | |
var Redux = require('redux') | |
function store_enhancer (options) { | |
return function (next) { | |
return function (reducer, initial_state) { | |
// console.log('initialization / state retrieval', initial_state); | |
var store = next(reducer, initial_state) | |
// listen for changes | |
store.subscribe(function () { | |
var state = store.getState() | |
// console.log('send state change off', state); | |
}) | |
return store | |
} | |
} | |
} | |
var Store = Redux.compose( | |
Redux.applyMiddleware(Debug(debug('redux'))), | |
store_enhancer() | |
)(Redux.createStore) | |
function reducer (state, action) { | |
switch (action.type) { | |
case 'CHANGE_NAME': | |
return assign(state, { | |
name: action.name | |
}) | |
default: | |
return state | |
} | |
} | |
var initial_state = { | |
name: 'Matt', | |
location: 'San Francisco, CA', | |
age: 26 | |
} | |
var store = Store(reducer, initial_state) | |
store.subscribe(function () { | |
// console.log('changed', store.getState()); | |
}) | |
store.dispatch({ | |
type: 'CHANGE_NAME', | |
name: 'Matthew' | |
}) | |
// console.log('output 1', store.getState()); | |
store.dispatch({ | |
type: 'CHANGE_NAME', | |
name: 'Marty' | |
}) | |
// console.log('output 2', store.getState()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Only downside with these functional frameworks is that they're unfamiliar to most JS developers, so they can be confusing at first. Layer ES6 on top of these concepts and it's downright confusing. Here's my attempt at breaking things out a bit to help me (and hopefully you) understand the crux of redux.