Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第 199 期《React Saga和Redux最佳实践》by 大鱼 #197

Open
bencode opened this issue Jul 9, 2018 · 0 comments
Open

第 199 期《React Saga和Redux最佳实践》by 大鱼 #197

bencode opened this issue Jul 9, 2018 · 0 comments

Comments

@bencode
Copy link

bencode commented Jul 9, 2018

PS: 不好意思,上次的轻分享忘记记录了,这里补一下。

redux能够较好地管理应用状态,但是对异步流程的处理能力有限,简单情况下(多数场景都应该是简单的)可以使用redux-promiseredux-thunk来完成。我们目前的应用如lego和magic都是使用了 redux-promise。

但更复杂的有副作用的场景就不能很好处理,而 redux-saga就是专门处理这种副作用场景的,它引入一个叫 effects 的概念,利用ES6的generator function语言特性来实现effects。effects是对reducer在处理副作用场景的一个补充。

现在实现层面有 reducer和effects,剩下来就是如果利用好这两者更好地表达应用,也就是最佳实践。
这里有一些讨论,它的成果就是 dva,dva引入了模型的概念,用来将应用的逻辑组织在一起,从而将逻辑和视图分开。 这里的逻辑主要包括三部分 1. 状态(state) 2. reducer 3. effects。这样我们就有了一种表达应用的最佳方式(目前来说)。

我觉得不管是否用到saga,dva的组织应用的方式都值得尝试, 应用中可用dva-core这个package方便集成到现有代码。

这里是分享中用到的示例,sagadva

注意到代码中模型的编写,是在使用时注入到app里面的,这样可以更方便地进行单元测试。

@bencode bencode changed the title 199期《React Saga和Redux最佳实践》by 大鱼 第 199 期《React Saga和Redux最佳实践》by 大鱼 Jul 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants