Skip to content

Instantly share code, notes, and snippets.

@matthewmueller
Created October 10, 2015 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewmueller/8ec5cfba57152d11fe1b to your computer and use it in GitHub Desktop.
Save matthewmueller/8ec5cfba57152d11fe1b to your computer and use it in GitHub Desktop.
redux reduxed
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());
@matthewmueller
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment