this.setState(paticalState,callback)过程

会调用 this.updater.enquenSetState(this,paticalState,callback,’setState’),
enquenSetState 内部调用了 ReactInstanceMap.get(component也就是this)获取到了fiber实例,然后基于performance.now()||Date.now()获取到currentTime,用createUpdate(currentTime)创建了update对象(单链表节点格式对象,最终调用 enquenUpdate(fiber,update)来update。
enquenUpdate源码

  1. //以下注释仅对 ClassComponent有效(即你的组件是继承React.Component)
  2. export function enqueueUpdate<State>(fiber: Fiber, update: Update<State>) {
  3. // Update queues are created lazily. Update queues被延迟创建(即这个组件没有update 我们没有必要给它fiber来创建这样属性)
  4. const alternate = fiber.alternate;
  5. let queue1;//设计它是为了指向current的updateQueue
  6. let queue2; //设计它是为了指向alternate 的updateQueue
  7. if (alternate === null) {//alternate为null,对于classcomponent第一次调用setState时alternate为null
  8. // There's only one fiber.
  9. queue1 = fiber.updateQueue;
  10. queue2 = null;
  11. if (queue1 === null) {//首次调用setState创建updateQueue,这时的memoizedState为初始值
  12. queue1 = fiber.updateQueue = createUpdateQueue(fiber.memoizedState);
  13. }
  14. } else {
  15. // There are two owners.
  16. queue1 = fiber.updateQueue;//current Fiber updateQueue
  17. queue2 = alternate.updateQueue;
  18. if (queue1 === null) {
  19. if (queue2 === null) {
  20. // Neither fiber has an update queue. Create new ones.
  21. //如果都为空,则分别创建,这个case我没有找到
  22. queue1 = fiber.updateQueue = createUpdateQueue(fiber.memoizedState);
  23. queue2 = alternate.updateQueue = createUpdateQueue(
  24. alternate.memoizedState,
  25. );
  26. } else {
  27. // Only one fiber has an update queue. Clone to create a new one.
  28. // 如果有一个有update Queue,则克隆一个
  29. queue1 = fiber.updateQueue = cloneUpdateQueue(queue2);
  30. }
  31. } else {
  32. if (queue2 === null) {
  33. // Only one fiber has an update queue. Clone to create a new one.
  34. queue2 = alternate.updateQueue = cloneUpdateQueue(queue1);
  35. } else {
  36. // Both owners have an update queue.
  37. }
  38. }
  39. }
  40. if (queue2 === null || queue1 === queue2) {
  41. // There's only a single queue.
  42. //一般发生在首次 setState
  43. appendUpdateToQueue(queue1, update);
  44. } else {
  45. // There are two queues. We need to append the update to both queues,
  46. // while accounting for the persistent structure of the list — we don't
  47. // want the same update to be added multiple times.
  48. // 翻译:如果存在两个queues,我们需要追加这个 update到这个两个 queues.
  49. //然而对于这种持久性结构的列表(updateQueue)需要保证一次update不能添加多次
  50. if (queue1.lastUpdate === null || queue2.lastUpdate === null) {
  51. // One of the queues is not empty. We must add the update to both queues.
  52. appendUpdateToQueue(queue1, update);
  53. appendUpdateToQueue(queue2, update);
  54. } else {
  55. // Both queues are non-empty. The last update is the same in both lists,
  56. // because of structural sharing. So, only append to one of the lists.
  57. appendUpdateToQueue(queue1, update);
  58. // But we still need to update the `lastUpdate` pointer of queue2.
  59. queue2.lastUpdate = update;
  60. }
  61. }
  62. if (__DEV__) {
  63. if (
  64. fiber.tag === ClassComponent &&
  65. (currentlyProcessingQueue === queue1 ||
  66. (queue2 !== null && currentlyProcessingQueue === queue2)) &&
  67. !didWarnUpdateInsideUpdate
  68. ) {
  69. warningWithoutStack(
  70. false,
  71. 'An update (setState, replaceState, or forceUpdate) was scheduled ' +
  72. 'from inside an update function. Update functions should be pure, ' +
  73. 'with zero side-effects. Consider using componentDidUpdate or a ' +
  74. 'callback.',
  75. );
  76. didWarnUpdateInsideUpdate = true;
  77. }
  78. }
  79. }

updater

当前版本下updater下挂在了四个api

  1. // react中打印this可见,最重要的就是enqueueSetState
  2. updater:{
  3. enqueueForceUpdate: ƒ (inst, callback)
  4. enqueueReplaceState: ƒ (inst, payload, callback)
  5. enqueueSetState: ƒ (inst, payload, callback)
  6. isMounted: ƒ isMounted(component)
  7. }

updater对象在整个react框架里是个单例。所有的update其实都是调用了这个对象的方法。