import {createStore} from 'redux'
function counter(state=0,action) {
switch (action.type) {
case 'add':
return state+1;
case 'reduce':
return state-1;
default:
return 10
}
}
//新建store
const store = createStore(counter);
const init = store.getState();
console.log(init);
//监听
function listener() {
const current = store.getState();
console.log(`现在有的${current}`);
}
store.subscribe(listener)
//派发事件 传递aciton
store.dispatch({type:'add'});
store.dispatch({type:'reduce'});
store.dispatch({type:'add'});
store.dispatch({type:'add'});
// action creator
export function add() {
return {type:ADD};
};
export function reduce() {
return {type:REDUCE};
};