Commit 0dae8b5
committed
Move all markRef calls into begin phase
Certain fiber types may have a ref attached to them. The main ones are
HostComponent and ClassComponent. During the render phase, we check if
a ref was passed to it, and if so, we schedule a Ref effect: `markRef`.
Currently, we're not consistent about whether we call `markRef` in the
begin phase or the complete phase. For some fiber types, I found that
`markRef` was called in both phases, causing redundant work.
After some investigation, I don't believe it's necessary to call
`markRef` in both the begin phase and the complete phase, as long as you
don't bail out before calling `markRef`.
I though that maybe it had to do with the
`attemptEarlyBailoutIfNoScheduledUpdates` branch, which is a fast path
that skips the regular begin phase if no new props, state, or context
were passed. But if the props haven't changed (referentially —
the `memo` and `shouldComponentUpdate` checks happen later), then it
follows that the ref couldn't have changed either. This is true even in
the old `createElement` runtime where `ref` is stored on the element
instead of as a prop, because there's no way to pass a new ref to an
element without also passing new props. You might argue this is a
leaky assumption, but since we're shifting ref to be just a regular prop
anyway, I think it's the correct way to think about it going forward.
I think the pattern of calling `markRef` in the complete phase may have
been left over from an earlier iteration of the implementation before
the bailout logic was structured like it is today.
So, I removed all the `markRef` calls from the complete phase. In the
case of ScopeComponent, which had no corresponding call in the begin
phase, I added one.
We already had a test that asserted that a ref is reattached even if
the component bails out, but I added some new ones to be extra safe.
The reason I'm changing this this is because I'm working on a different
change to move the ref handling logic in `coerceRef` to happen in render
phase of the component that accepts the ref, instead of during the
parent's reconciliation.1 parent 59831c9 commit 0dae8b5
File tree
3 files changed
+90
-35
lines changed- packages/react-reconciler/src
- __tests__
3 files changed
+90
-35
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1007 | 1007 | | |
1008 | 1008 | | |
1009 | 1009 | | |
| 1010 | + | |
| 1011 | + | |
1010 | 1012 | | |
1011 | 1013 | | |
1012 | 1014 | | |
| |||
3531 | 3533 | | |
3532 | 3534 | | |
3533 | 3535 | | |
3534 | | - | |
| 3536 | + | |
3535 | 3537 | | |
3536 | 3538 | | |
3537 | 3539 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
79 | | - | |
80 | 78 | | |
81 | 79 | | |
82 | 80 | | |
| |||
186 | 184 | | |
187 | 185 | | |
188 | 186 | | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | 187 | | |
194 | 188 | | |
195 | 189 | | |
| |||
1083 | 1077 | | |
1084 | 1078 | | |
1085 | 1079 | | |
1086 | | - | |
1087 | | - | |
1088 | | - | |
1089 | 1080 | | |
1090 | 1081 | | |
1091 | 1082 | | |
| |||
1120 | 1111 | | |
1121 | 1112 | | |
1122 | 1113 | | |
1123 | | - | |
1124 | | - | |
1125 | | - | |
1126 | 1114 | | |
1127 | 1115 | | |
1128 | 1116 | | |
| |||
1194 | 1182 | | |
1195 | 1183 | | |
1196 | 1184 | | |
1197 | | - | |
1198 | | - | |
1199 | | - | |
1200 | | - | |
1201 | 1185 | | |
1202 | 1186 | | |
1203 | 1187 | | |
| |||
1232 | 1216 | | |
1233 | 1217 | | |
1234 | 1218 | | |
1235 | | - | |
1236 | | - | |
1237 | | - | |
1238 | | - | |
1239 | | - | |
1240 | 1219 | | |
1241 | 1220 | | |
1242 | 1221 | | |
| |||
1254 | 1233 | | |
1255 | 1234 | | |
1256 | 1235 | | |
1257 | | - | |
1258 | | - | |
1259 | | - | |
1260 | | - | |
1261 | 1236 | | |
1262 | 1237 | | |
1263 | 1238 | | |
| |||
1310 | 1285 | | |
1311 | 1286 | | |
1312 | 1287 | | |
1313 | | - | |
1314 | | - | |
1315 | | - | |
1316 | | - | |
1317 | | - | |
1318 | 1288 | | |
1319 | 1289 | | |
1320 | 1290 | | |
| |||
1739 | 1709 | | |
1740 | 1710 | | |
1741 | 1711 | | |
1742 | | - | |
| 1712 | + | |
| 1713 | + | |
1743 | 1714 | | |
1744 | 1715 | | |
1745 | 1716 | | |
1746 | 1717 | | |
| 1718 | + | |
| 1719 | + | |
1747 | 1720 | | |
1748 | 1721 | | |
1749 | | - | |
1750 | | - | |
1751 | | - | |
1752 | 1722 | | |
1753 | 1723 | | |
1754 | 1724 | | |
| |||
Lines changed: 83 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
0 commit comments