react-redux 和 redux-saga 小结

react-redux 将 store 绑定到 props 上,便于全局调用。

redux-saga 是将 redux 的同步转换为异步。

注:

dispatch 到 saga , saga 匹配行为的 type,调用接口请求数据,并将数据 通过 dispatch 传给 redux。

redux 匹配 type 并将数据绑定到 props 上

思路:

(1)routes 下的页面触发 action

this.props.dispatch({
  type:"xxx", // 行为名称
  payload: yyy
})

(2)saga 下的异步 redux 进行匹配

找到对应的 fork(reducer)

在 reducer 中调用整体的也是唯一的 action

yield takeLatest("xxx",(yyy) => functionName(yyy))

在 functionName 中使用 put 代替 dispatch

yield put({
  type:"aaa", // 数据type
  payload: bbb
})

(3)redux 下的同步 redux 进行匹配

找到对应的 reducer

在 reducer 中进行 type 匹配

case 匹配 type , return 出去的数据 需要 先 

const initstate = { ccc: ddd }

初始化。

将获取的 数据 return 到 props 上

(4)全局获取 store 上的数据

const { ccc } = this.props;

.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值