原始的redux
作者:
现_状 | 来源:发表于
2019-03-28 15:43 被阅读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:'减机关枪'});
本文标题:原始的redux
本文链接:https://www.haomeiwen.com/subject/hsaqbqtx.html
网友评论