We believe programming should be fun and light, not stern and stressful. It's cool to be cute; using serious words without explaining them doesn't make for better results - if anything it scares people off. We don't want to be scary, we want to be nice and fun, and then casually be the best choice around. Real casually.
We believe frameworks should be disposable, and components recyclable. We don't want a web where walled gardens jealously compete with one another. By making the DOM the lowest common denominator, switching from one framework to another becomes frictionless. Choo is modest in its design; we don't believe it will be top of the class forever, so we've made it as easy to toss out as it is to pick up.
We don't believe that bigger is better. Big APIs, large complexities, long files - we see them as omens of impending userland complexity. We want everyone on a team, no matter the size, to fully understand how an application is laid out. And once an application is built, we want it to be small, performant and easy to reason about. All of which makes for easy to debug code, better results and super smiley faces.
Dva 是什么
dva 首先是一个基于 redux 和 redux-saga 的数据流方案,然后为了简化开发体验,dva 还额外内置了 react-router 和 fetch,所以也可以理解为一个轻量级的应用框架。
Dva 解决的问题
Dva 的优势
Dva 的劣势
Dva 的适用场景
Dva 核心概念
基于 Redux 的基本概念。包括:
其他概念
Dva 应用最简结构
不带 Model
带 Model
Dva 的底层原理和部分关键实现
背景介绍
dva
dva 做了三件比较重要的事情:
我们同时可以发现 app 是通过create(opts, createOpts)进行初始化的,其中opts是暴露给使用者的配置,createOpts是暴露给开发者的配置,真实的create方法在dva-core中实现
dva-core
dva-core 则完成了核心功能:
通过 create 方法完成 app 实例的构造,并暴露use、model和start三个接口
通过 start 方法完成
dva-core create
作用: 完成 app 实例的构造,并暴露use、model和start三个接口
dva-core start
作用:
路由
在前面的dva.start方法中我们看到了createOpts,并了解到在dva-core的start中的不同时机调用了对应方法。
其中initialReducer和setupMiddlewares在初始化store时调用,然后才调用setupApp
可以看见针对router相关的reducer和中间件配置,其中connectRouter和routerMiddleware均使用了connected-react-router这个库,其主要思路是:把路由跳转也当做了一种特殊的action。
Dva 与 React、React-Redux、Redux-Saga 之间的差异
原生 React
按照 React 官方指导意见, 如果多个 Component 之间要发生交互, 那么状态(即: 数据)就维护在这些 Component 的最小公约父节点上, 也即是
React-Redux
与上图相比, 几个明显的改进点:
这样一来, 各个部分各司其职, 耦合度更低, 复用度更高, 扩展性更好。
Redux-Saga
因为我们可以使用 Middleware 拦截 action, 这样一来异步的网络操作也就很方便了, 做成一个 Middleware 就行了, 这里使用 redux-saga 这个类库, 举个栗子:
Dva
有了前面三步的铺垫, Dva 的出现也就水到渠成了, 正如 Dva 官网所言, Dva 是基于 React + Redux + Saga 的最佳实践, 对于提升编码体验有三点贡献:
约定大于配置
Dva 背后值得学习的思想
附录
Why dva and what's dva
支付宝前端应用架构的发展和选择
React + Redux 最佳实践
Dva 概念
Dva 入门课
Dva 源码解析
Dva 源码实现
Dva 源码分析