subscribe()
import { createStore } from "redux"
function counter (state=0,action){
switch(action.type){
case "加機關槍":
return state + 1
case "減機關槍":
return state - 1
case "搶機關槍":
return state * 1000
default:
return 10
}
}
const store = createStore(counter)
const init = store.getState()
console.log(init)
function listener(){
const cur = store.getState()
console.log(`現在有機槍 ${cur}把`)
}
store.subscribe(listener)
store.dispatch({type: "加機關槍"})
store.dispatch({type: "加機關槍"})
store.dispatch({type: "減機關槍"})
store.dispatch({type: "搶機關槍"})