Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@keenwon
Created June 29, 2016 03:43
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 keenwon/dfc68a4f9d880de920f9d1c0050699c6 to your computer and use it in GitHub Desktop.
Save keenwon/dfc68a4f9d880de920f9d1c0050699c6 to your computer and use it in GitHub Desktop.
redux middleware test
<!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