Skip to content

Instantly share code, notes, and snippets.

@mindfulme
Created May 29, 2018 13:41
Show Gist options
  • Save mindfulme/fba025c4f0c28c04759b921edf197080 to your computer and use it in GitHub Desktop.
Save mindfulme/fba025c4f0c28c04759b921edf197080 to your computer and use it in GitHub Desktop.
redux1-5, error handling, logger, middleware
import React, { Component } from 'react';
import { applyMiddleware, createStore } from 'redux';
const reducer = function(state, action) {
if (action.type==='INC') {
return ++state+1;
}
else if (action.type==='DEC') {
return --state-action.payload;
}
else if (action.type === 'E') {
throw new Error("AAA!!!!");
}
return state;
}
const logger = (store) => (next) => (action) => {
console.log('action fired ', action);
next(action);
}
const error = (store) => (next) => (action) => {
try{
next(action);
}
catch(e) {
console.log('AHHH!', e);
}
}
const middleware = applyMiddleware(logger, error);
const store = createStore(reducer, 0, middleware);
store.subscribe(() => {
console.log("store changed", store.getState());
})
store.dispatch({type: 'INC', payload: 1})
store.dispatch({type: 'INC', payload: 1})
store.dispatch({type: 'INC', payload: 1})
store.dispatch({type: 'DEC', payload: 10})
store.dispatch({type: 'E', payload: 10})
class App extends Component {
render() {
return (
<div>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment