Created
June 29, 2016 03:43
-
-
Save keenwon/dfc68a4f9d880de920f9d1c0050699c6 to your computer and use it in GitHub Desktop.
redux middleware test
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
<!doctype HTML> | |
<html> | |
<head> | |
<title>Redux Test</title> | |
</head> | |
<body> | |
<script src="https://npmcdn.com/redux@3.5.2/dist/redux.js"></script> | |
<script> | |
// actions | |
const ADD = 'ADD'; | |
function add() { | |
return { | |
type: ADD | |
} | |
} | |
// reducer | |
var initialState = { | |
count: 0 | |
} | |
function reducer(state = initialState, action) { | |
switch (action.type) { | |
case ADD: | |
return { | |
count: ++state.count | |
}; | |
default: | |
return state; | |
} | |
} | |
// store | |
var applyMiddleware = Redux.applyMiddleware, | |
createStore = Redux.createStore; | |
var middleware1 = store => next => action => { | |
console.log('=====middleware1====='); | |
console.log(store); | |
console.log(next); | |
console.log(action); | |
console.log('=====middleware1====='); | |
next(action); | |
}; | |
var middleware2 = store => next => action => { | |
console.log('=====middleware2====='); | |
console.log(store); | |
console.log(next); | |
console.log(action); | |
console.log('=====middleware2====='); | |
next(action); | |
}; | |
const store = createStore( | |
reducer, | |
applyMiddleware(middleware1, middleware2) | |
); | |
console.log(store.getState()); | |
store.dispatch(add()); | |
console.log(store.getState()); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment