原始的redux
2019-03-28 本文已影响0人
现_状
import { createStore } from 'redux';
//1.通过reducer新建store
//2.根据老的state和action 生成新的state
//下面这个函数其实就是reducer的作用
function counter(state=0,action){
switch(action.type){
case '加机关枪':
return state+1
case '减机关枪':
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}`)
}
//用subscribe监听每次修改情况
store.subscribe(listener)
//dispatch派发事件 传递action
store.dispatch({type:'加机关枪'});
store.dispatch({type:'加机关枪'});
store.dispatch({type:'减机关枪'});