Thinking in Redux 笔记

本文深入解析Redux中的三种行动类型:Command、Event与Document行动。Command行动用于启动服务器API调用,Event行动负责通知状态变化,Document行动则用于更新状态中的数据。文章详细介绍了每种行动的使用场景和构造方式。

Redux is all about actions

ActionCreator
Action
Middleware
Reducer

action 只是一个 javascript object

只是发起action的主体是不同的
1。 可以从使用者操作发起
2。 或是middleware发起
二个不同的地方发起

action分成三类

  1. Command actions
  2. Event actions
  3. Document actions

Command actions

command action可以当成api call to the server
command action 永远不会进到reducer
command action 通常用动词
GET FIND REMOVE FETCH

const FETCH_BOOKS={ 
  type: 'FETCH_BOOKS', 
  payload: {
    query: 'redux',
  },
  meta: { 
    timeout: 3000
  }
}

Event actions

event action 用来通知发生变动
event action 只被middleware处理

const API_REQUEST_PENDING={ 
  type: 'API_REQUEST_PENDING',
}
const ROUTING_STARTED = {
  type:'ROUTING_STARTED',
  payload:{
    userId:'123456'
  },
  meta:{
    from:'/home',
    to:'/dashboard'
  }
}

Document actions

document action是拿来放资料的,淮备用来存到state去的
document action只被reducer处理

const SET_BOOK={ 
  type: 'SET_BOOK', 
  payload: [{...},{...}]    
}

const UPDATE_BOOK={ 
  type: 'UPDATE_BOOK', 
  payload: [{...},{...}]
}

const REMOVE_BOOK={ 
  type: 'REMOVE_BOOK', 
  payload: 2545852
}

流程通常由command action 开始
烛发event 和其它 command actions
最后由document action结束
来完成一次流程

Action typeIntentionDispatched byProcessed by
Commandstart a procedureUI middlewaremiddleware
EventNotify about changeMiddlewareMiddleware
DocumentWrite data to stateMiddlewareReducer

Action Payload and Metadata

Action Construction

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值