import {createStore} from "redux"; //引入生成store的方法 //配置reducer规则 function counter(state=0,action) { switch(action.type){ case "red": return state+1; case "green": return state-1; default: return 10; } } //通过reducer来生成store const store=createStore(counter); const init =store.getState();//获取到当前的state console.log(init); //订阅函数 function listen(){ var counter = store.getState(); console.log(`now num is ${counter}`); } store.subscribe(listen) //设置监听函数,只要dispath 都会触发 该方法 //派发事件,生成新的state store.dispatch({type:"red"}); store.dispatch({type:"red"}); store.dispatch({type:"green"});
//简单来说就是 dispatch替代了setState