Redux Saga实践

本文详述了Redux Saga在项目中的应用,解释了Redux Saga作为处理副作用的库,如何简化复杂的异步逻辑。通过一个表单提交的例子,展示了如何使用Saga将前端展示层、业务层和数据层分离,使得代码结构更加清晰。同时提到了中间件、生成器等概念,以及takeEvery和takeLatest等Saga处理副作用的方法。尽管Redux Saga的学习曲线较陡峭,但能提高代码的可读性和复用性。
摘要由CSDN通过智能技术生成

本文用以记录从调研Redux Saga,到应用到项目中的一些收获。

项目链接: https://github.com/fanxiao168/React-todoList
什么是Redux Saga

官网解释 来自:https://github.com/redux-saga/redux-saga

redux-saga is a library that aims to make side effects (i.e. asynchronous things like data fetching and impure things like accessing the browser cache) in React/Redux applications easier and better.

The mental model is that a saga is like a separate thread in your application that’s solely responsible for side effects. redux-saga is a redux middleware, which means this thread can be started, paused and cancelled from the main application with normal redux actions, it has access to the full redux application state and it can dispatch redux actions as well.

刚开始了解Saga时,看官方解释,并不是很清楚到底是什么?Saga的副作用(side effects)到底是什么?

通读了官方文档后,大概了解到,副作用就是在action触发reduser之后执行的一些动作, 这些动作包括但不限于,连接网络,io读写,触发其他action。并且,因为Sage的副作用是通过redux的action触发的,每一个action,sage都会像reduser一样接收到。并且通过触发不同的action, 我们可以控制这些副作用的状态, 例如,启动,停止,取消。

所以,我们可以理解为Sage是一个可以用来处理复杂的异步逻辑的模块,并且由redux的action触发。

使用Saga解决的问题

最初,在开始探究Saga之前,我们是希望寻求一种方式来隔离开应用前端的展现层,业务层和数据层。 大概想法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值