react-redux基础

文章结合一个官方示例理解更简单地址:https://github.com/GuoMouMo/ReduxSummary



首先redux有哪些基本的知识:store、state、action、reducer。

1、 store中含有哪些属性
     store:Object{
           dispatch:function dispatch(action) 
getState:function getState()
replaceReducer:function replaceReducer(nextReducer)
subscript:subscribe(listener)
Symbol(observable):function observable()
}
     以上属性功能不多解释

2、 state的获取及含有什么属性
     state = store.getState();  //获取state
     state:Object{
         //state的属性就是reducer中的属性。reducer是一种根据action产生新state的属性方法  
}

3、 action是什么,怎么产生,一般怎么使用
     action是一种描述事件的对象。一般action如下
     action:{
   type: "THING",    //描述什么事件
text              //描述事件中产生的后果
}
     一般通过自定义的方法产生action,如todo(text){
                                       return {
           type: "TING",
       text
       }
  }
    一般这样使用store.dispatch(todo(text))

4、 reducer是什么,有什么用
     reducer是根据action产生新的state的一种方法,接受两个参数 (老的state,action)。
     当store.dispatch(action)接受新的action时自动调用reducer,
     第一次使用时是在创建store时,store = createStore(reducer)



总结、1、只有一个store,包含所有时段的数据,通过createStore(reducer)创建

           2、state通过store.getState()获得,一个view对应一个state 

           3、Action view改变通过Action获得新的state;Action的格式一般为{
                                                             type:"ADD",   //标识
           payload:"learnRedux"   //行为
      }
           4、store.dispatch(Action) View通过此方法发送Action;

           5、Reducer 接受Action和state Reducer(state,Action){
                                                     switch(action.type){         //通过行为产生新的state
            return new_state;

                                                     }
      }

           6、通过store = createStore(reducer)创建一个store,当通过store.dispatch传进来一个action就会自动调用reducer()生成一个新的state;

           7、store.subscript() 监听state的变化,当state改变时触发。store.subscript返回一个函数,当调用这个函数是即可解除监听,有点像定
 时器。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值