译者序:本文是 React 核心开发者、有 React API 终结者之称的 Sebastian Markbåge 撰写,阐述了他设计 React 的初衷。阅读此文,你能站在更高的高度思考 React 的过去、现在和未来。原文地址:https://github.com/reactjs/react-basic

我写此文是想正式地阐述我心中 React 的心智模型。目的是解释为什么我们会这样设计 React,同时你也可以根据这些论点反推出 React。

不可否认,此文中的部分论据或前提尚存争议,而且部分示例的设计可能存在 bug 或疏忽。这只是正式确定它的最初阶段。如果你有更好的完善它的想法可以随时提交 pull request。本文不会介绍框架细节中的奇技淫巧,相信这样能提纲挈领,让你看清 React 由简单到复杂的设计过程

React.js 的真实实现中充满了具体问题的解决方案,渐进式的解法,算法优化,历史遗留代码,debug 工具以及其他一些可以让它真的具有高可用性的内容。这些代码可能并不稳定,因为未来浏览器的变化和功能权重的变化随时面临改变。所以具体的代码很难彻底解释清楚。

我偏向于选择一种我能完全 hold 住的简洁的心智模型来作介绍。

变换(Transformation)

设计 React 的核心前提是认为 UI 只是把数据通过映射关系变换成另一种形式的数据。同样的输入必会有同样的输出。这恰好就是纯函数。

  1. function NameBox(name) {
  2. return { fontWeight: 'bold', labelContent: name };
  3. }
  1. 'Sebastian Markbåge' ->
  2. { fontWeight: 'bold', labelContent: 'Sebastian Markbåge' };

抽象(Abstraction)

你不可能仅用一个函数就能实现复杂的 UI。重要的是,你需要把 UI 抽象成多个隐藏内部细节,又可复用的函数。通过在一个函数中调用另一个函数来实现复杂的 UI,这就是抽象。

  1. function FancyUserBox(user) {
  2. return {
  3. borderStyle: '1px solid blue',
  4. childContent: [
  5. 'Name: ',
  6. NameBox(user.firstName + ' ' + user.lastName)
  7. ]
  8. };
  9. }
  1. { firstName: 'Sebastian', lastName: 'Markbåge' } ->
  2. {
  3. borderStyle: '1px solid blue',
  4. childContent: [
  5. 'Name: ',
  6. { fontWeight: 'bold', labelContent: 'Sebastian Markbåge' }
  7. ]
  8. };

组合(Composition)

为了真正达到重用的特性,只重用叶子然后每次都为他们创建一个新的容器是不够的。你还需要可以包含其他抽象的容器再次进行组合。我理解的“组合”就是将两个或者多个不同的抽象合并为一个

  1. function FancyBox(children) {
  2. return {
  3. borderStyle: '1px solid blue',
  4. children: children
  5. };
  6. }
  7. function UserBox(user) {
  8. return FancyBox([
  9. 'Name: ',
  10. NameBox(user.firstName + ' ' + user.lastName)
  11. ]);
  12. }

状态(State)

UI 不单单是对服务器端或业务逻辑状态的复制。实际上还有很多状态是针对具体的渲染目标。举个例子,在一个 text field 中打字。它不一定要复制到其他页面或者你的手机设备。滚动位置这个状态是一个典型的你几乎不会复制到多个渲染目标的。

我们倾向于使用不可变的数据模型。我们把可以改变 state 的函数串联起来作为原点放置在顶层。

  1. function FancyNameBox(user, likes, onClick) {
  2. return FancyBox([
  3. 'Name: ', NameBox(user.firstName + ' ' + user.lastName),
  4. 'Likes: ', LikeBox(likes),
  5. LikeButton(onClick)
  6. ]);
  7. }
  8. // 实现细节
  9. var likes = 0;
  10. function addOneMoreLike() {
  11. likes++;
  12. rerender();
  13. }
  14. // 初始化
  15. FancyNameBox(
  16. { firstName: 'Sebastian', lastName: 'Markbåge' },
  17. likes,
  18. addOneMoreLike
  19. );

注意:本例更新状态时会带来副作用(addOneMoreLike 函数中)。我实际的想法是当一个“update”传入时我们返回下一个版本的状态,但那样会比较复杂。此示例待更新

记忆化 (Memoization)

对于纯函数,使用相同的参数一次次调用未免太浪费资源。我们可以创建一个函数的 memorized 版本,用来追踪最后一个参数和结果。这样如果我们继续使用同样的值,就不需要反复执行它了。

  1. function memoize(fn) {
  2. var cachedArg;
  3. var cachedResult;
  4. return function(arg) {
  5. if (cachedArg === arg) {
  6. return cachedResult;
  7. }
  8. cachedArg = arg;
  9. cachedResult = fn(arg);
  10. return cachedResult;
  11. };
  12. }
  13. var MemoizedNameBox = memoize(NameBox);
  14. function NameAndAgeBox(user, currentTime) {
  15. return FancyBox([
  16. 'Name: ',
  17. MemoizedNameBox(user.firstName + ' ' + user.lastName),
  18. 'Age in milliseconds: ',
  19. currentTime - user.dateOfBirth
  20. ]);
  21. }

列表(Lists)

大部分 UI 都是展示列表数据中不同 item 的列表结构。这是一个天然的层级。

为了管理列表中的每一个 item 的 state ,我们可以创造一个 Map 容纳具体 item 的 state。

  1. function UserList(users, likesPerUser, updateUserLikes) {
  2. return users.map(user => FancyNameBox(
  3. user,
  4. likesPerUser.get(user.id),
  5. () => updateUserLikes(user.id, likesPerUser.get(user.id) + 1)
  6. ));
  7. }
  8. var likesPerUser = new Map();
  9. function updateUserLikes(id, likeCount) {
  10. likesPerUser.set(id, likeCount);
  11. rerender();
  12. }
  13. UserList(data.users, likesPerUser, updateUserLikes);

注意:现在我们向 FancyNameBox 传了多个不同的参数。这打破了我们的 memoization 因为我们每次只能存储一个值。更多相关内容在下面。

连续性(Continuations)

不幸的是,自从 UI 中有太多的列表,明确的管理就需要大量的重复性样板代码。

我们可以通过推迟一些函数的执行,进而把一些模板移出业务逻辑。比如,使用“柯里化”(JavaScript 中的 bind)。然后我们可以从核心的函数外面传递 state,这样就没有样板代码了。

下面这样并没有减少样板代码,但至少把它从关键业务逻辑中剥离。

  1. function FancyUserList(users) {
  2. return FancyBox(
  3. UserList.bind(null, users)
  4. );
  5. }
  6. const box = FancyUserList(data.users);
  7. const resolvedChildren = box.children(likesPerUser, updateUserLikes);
  8. const resolvedBox = {
  9. ...box,
  10. children: resolvedChildren
  11. };

State Map

之前我们知道可以使用组合避免重复执行相同的东西这样一种重复模式。我们可以把执行和传递 state 逻辑挪动到被复用很多的低层级的函数中去。

  1. function FancyBoxWithState(
  2. children,
  3. stateMap,
  4. updateState
  5. ) {
  6. return FancyBox(
  7. children.map(child => child.continuation(
  8. stateMap.get(child.key),
  9. updateState
  10. ))
  11. );
  12. }
  13. function UserList(users) {
  14. return users.map(user => {
  15. continuation: FancyNameBox.bind(null, user),
  16. key: user.id
  17. });
  18. }
  19. function FancyUserList(users) {
  20. return FancyBoxWithState.bind(null,
  21. UserList(users)
  22. );
  23. }
  24. const continuation = FancyUserList(data.users);
  25. continuation(likesPerUser, updateUserLikes);

Memoization Map

一旦我们想在一个 memoization 列表中 memoize 多个 item 就会变得很困难。因为你需要制定复杂的缓存算法来平衡调用频率和内存占有率。

还好 UI 在同一个位置会相对的稳定。相同的位置一般每次都会接受相同的参数。这样以来,使用一个集合来做 memoization 是一个非常好用的策略。

我们可以用对待 state 同样的方式,在组合的函数中传递一个 memoization 缓存。

  1. function memoize(fn) {
  2. return function(arg, memoizationCache) {
  3. if (memoizationCache.arg === arg) {
  4. return memoizationCache.result;
  5. }
  6. const result = fn(arg);
  7. memoizationCache.arg = arg;
  8. memoizationCache.result = result;
  9. return result;
  10. };
  11. }
  12. function FancyBoxWithState(
  13. children,
  14. stateMap,
  15. updateState,
  16. memoizationCache
  17. ) {
  18. return FancyBox(
  19. children.map(child => child.continuation(
  20. stateMap.get(child.key),
  21. updateState,
  22. memoizationCache.get(child.key)
  23. ))
  24. );
  25. }
  26. const MemoizedFancyNameBox = memoize(FancyNameBox);

代数效应(Algebraic Effects)

多层抽象需要共享琐碎数据时,一层层传递数据非常麻烦。如果能有一种方式可以在多层抽象中快捷地传递数据,同时又不需要牵涉到中间层级,那该有多好。React 中我们把它叫做“context”。

有时候数据依赖并不是严格按照抽象树自上而下进行。举个例子,在布局算法中,你需要在实现他们的位置之前了解子节点的大小。

现在,这个例子有一点超纲。我会使用 代数效应 这个由我发起的 ECMAScript 新特性提议。如果你对函数式编程很熟悉,它们在避免由 monad 强制引入的仪式一样的编码。

  1. function ThemeBorderColorRequest() { }
  2. function FancyBox(children) {
  3. const color = raise new ThemeBorderColorRequest();
  4. return {
  5. borderWidth: '1px',
  6. borderColor: color,
  7. children: children
  8. };
  9. }
  10. function BlueTheme(children) {
  11. return try {
  12. children();
  13. } catch effect ThemeBorderColorRequest -> [, continuation] {
  14. continuation('blue');
  15. }
  16. }
  17. function App(data) {
  18. return BlueTheme(
  19. FancyUserList.bind(null, data.users)
  20. );
  21. }