Redux01 续集

subscribe()

import { createStore } from "redux"

/*新建stoore
*通过reducer 建立
*根据老的状态和action,生成新的state   */

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)

/*新建store 结束*/

const init = store.getState()

console.log(init)

function listener(){
    const cur = store.getState()
    console.log(`现在有机枪  ${cur}把`)
}

store.subscribe(listener)

/*  派发事件 传递action    */

store.dispatch({type: "加机关枪"})


store.dispatch({type: "加机关枪"})


store.dispatch({type: "减机关枪"})


store.dispatch({type: "抢机关枪"})
















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章