Created
May 29, 2018 13:41
-
-
Save mindfulme/fba025c4f0c28c04759b921edf197080 to your computer and use it in GitHub Desktop.
redux1-5, error handling, logger, middleware
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
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