Commit 6a1c6f8
committed
Allow useReducer to bail out of rendering by returning previous state
This is conceptually similar to `shouldComponentUpdate`, except because
there could be multiple useReducer (or useState) Hooks in a single
component, we can only bail out if none of the Hooks produce a new
value. We also can't bail out if any the other types of inputs — state
and context — have changed.
These optimizations rely on the constraint that components are pure
functions of props, state, and context.
In some cases, we can bail out without entering the render phase by
eagerly computing the next state and comparing it to the current one.
This only works if we are absolutely certain that the queue is empty at
the time of the update. In concurrent mode, this is difficult to
determine, because there could be multiple copies of the queue and we
don't know which one is current without doing lots of extra work, which
would defeat the purpose of the optimization. However, in our
implementation, there are at most only two copies of the queue, and if
*both* are empty then we know that the current queue must be.1 parent f290138 commit 6a1c6f8
File tree
7 files changed
+563
-102
lines changed- packages/react-reconciler/src
- __tests__
7 files changed
+563
-102
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
144 | | - | |
| 144 | + | |
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
| 240 | + | |
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
| |||
403 | 403 | | |
404 | 404 | | |
405 | 405 | | |
406 | | - | |
| 406 | + | |
407 | 407 | | |
408 | 408 | | |
409 | 409 | | |
| |||
704 | 704 | | |
705 | 705 | | |
706 | 706 | | |
707 | | - | |
| 707 | + | |
708 | 708 | | |
709 | 709 | | |
710 | 710 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| |||
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
241 | 240 | | |
242 | 241 | | |
243 | 242 | | |
244 | | - | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
245 | 251 | | |
246 | 252 | | |
247 | | - | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
248 | 269 | | |
249 | | - | |
250 | 270 | | |
251 | | - | |
252 | | - | |
253 | 271 | | |
254 | 272 | | |
255 | 273 | | |
| |||
350 | 368 | | |
351 | 369 | | |
352 | 370 | | |
353 | | - | |
354 | | - | |
355 | 371 | | |
356 | 372 | | |
357 | 373 | | |
| |||
506 | 522 | | |
507 | 523 | | |
508 | 524 | | |
509 | | - | |
510 | 525 | | |
511 | 526 | | |
512 | 527 | | |
513 | | - | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
514 | 536 | | |
515 | 537 | | |
516 | | - | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
517 | 554 | | |
518 | | - | |
519 | 555 | | |
520 | | - | |
521 | | - | |
522 | 556 | | |
523 | 557 | | |
524 | 558 | | |
| |||
1063 | 1097 | | |
1064 | 1098 | | |
1065 | 1099 | | |
1066 | | - | |
1067 | 1100 | | |
1068 | 1101 | | |
1069 | 1102 | | |
| |||
1091 | 1124 | | |
1092 | 1125 | | |
1093 | 1126 | | |
1094 | | - | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
1095 | 1135 | | |
1096 | | - | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
1097 | 1144 | | |
1098 | | - | |
1099 | | - | |
1100 | 1145 | | |
1101 | 1146 | | |
1102 | 1147 | | |
| |||
1147 | 1192 | | |
1148 | 1193 | | |
1149 | 1194 | | |
1150 | | - | |
1151 | 1195 | | |
1152 | 1196 | | |
1153 | 1197 | | |
| |||
1647 | 1691 | | |
1648 | 1692 | | |
1649 | 1693 | | |
1650 | | - | |
| 1694 | + | |
1651 | 1695 | | |
1652 | 1696 | | |
| 1697 | + | |
| 1698 | + | |
1653 | 1699 | | |
1654 | 1700 | | |
1655 | 1701 | | |
| |||
1680 | 1726 | | |
1681 | 1727 | | |
1682 | 1728 | | |
1683 | | - | |
1684 | | - | |
1685 | | - | |
1686 | | - | |
1687 | | - | |
| 1729 | + | |
| 1730 | + | |
| 1731 | + | |
| 1732 | + | |
| 1733 | + | |
| 1734 | + | |
1688 | 1735 | | |
1689 | 1736 | | |
1690 | 1737 | | |
| |||
1767 | 1814 | | |
1768 | 1815 | | |
1769 | 1816 | | |
| 1817 | + | |
| 1818 | + | |
| 1819 | + | |
1770 | 1820 | | |
1771 | 1821 | | |
1772 | 1822 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
86 | 85 | | |
87 | 86 | | |
88 | 87 | | |
| |||
728 | 727 | | |
729 | 728 | | |
730 | 729 | | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
735 | | - | |
736 | | - | |
737 | | - | |
738 | | - | |
739 | | - | |
740 | | - | |
741 | | - | |
742 | | - | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
743 | 734 | | |
744 | 735 | | |
745 | 736 | | |
| |||
0 commit comments