zoukankan      html  css  js  c++  java
  • redux的createStore

    function createStore(reducer,initialState){
        let state = initialState || undefined
        let listener = []
        function getState(){
            return state
        }
        function subscribe(cb){
            listener.push(cb)
            return function(){
                let index = listener.indexOf(cb)
                listener.splice(index,1)
            }
        }
        function dispatch(action){
            state = reducer(state,action)
            listener.forEach((cb)=>{
                cb()
            })
            return action
        }
        dispatch({type:'@redux'})
        return {
            getState,
            subscribe,
            dispatch
        }
    }
    
    function reducer(state,action){
        switch(action.type){
            case '1' :
                return 'bai'
            case '2' :
                return 'ming'
            default :
                return state || '^^__^^'
        }
    }
    let store = createStore(reducer,25)
    console.log(store.getState())
    function render(){
        console.log(store.getState())
    }
    let unsubscribe = store.subscribe(render)
    console.log(store.dispatch({type:'1'}))
    unsubscribe()
    console.log(store.dispatch({type:2}))
  • 相关阅读:
    VijosP1274:神秘的咒语
    2009年浙大 :找出直系亲属
    django用户信息扩展
    缓存
    自定义认证
    自定义admin
    权限的配置和使用
    form表单
    过滤器 自定义查询
    中间件
  • 原文地址:https://www.cnblogs.com/MDGE/p/14367394.html
Copyright © 2011-2022 走看看