Hardened the hooks dehydration check
Brian Vaughn committed
Jun 18, 2019 at 08:47 UTC
6298fd424865811a5af655f01ee8e791c9298fc5
3 files changed
+24
-10
src/__tests__/__snapshots__/inspectedElementContext-test.js.snap
+12
-4
@@ -129,7 +129,9 @@ exports[`InspectedElementContext should not dehydrate nested values until explic
129
"id": 0,
130
"isStateEditable": true,
131
"name": "State",
132
- "value": {},
132
+ "value": {
133
+ "foo": {}
134
+ },
135
"subHooks": []
136
}
137
],
@@ -153,7 +155,9 @@ exports[`InspectedElementContext should not dehydrate nested values until explic
155
"id": 0,
156
"isStateEditable": true,
157
"name": "State",
156
- "value": {},
158
+ "value": {
159
+ "foo": {}
160
+ },
161
"subHooks": []
162
}
163
],
@@ -181,7 +185,9 @@ exports[`InspectedElementContext should not dehydrate nested values until explic
185
"id": 0,
186
"isStateEditable": true,
187
"name": "State",
184
- "value": {},
188
+ "value": {
189
+ "foo": {}
190
+ },
191
"subHooks": []
192
}
193
],
@@ -213,7 +219,9 @@ exports[`InspectedElementContext should not dehydrate nested values until explic
219
"id": 0,
220
"isStateEditable": true,
221
"name": "State",
216
- "value": {},
222
+ "value": {
223
+ "foo": {}
224
+ },
225
"subHooks": []
226
}
227
],
src/backend/renderer.js
+12
-3
@@ -2156,9 +2156,18 @@ export function attach(
2156
// Dehydrating the 'subHooks' property makes the HooksTree UI a lot more complicated,
2157
// so it's easiest for now if we just don't break on this boundary.
2158
// We can always dehydrate a level deeper (in the value object).
2159
- // TODO (hydration) This check depends on a LEVEL_THRESHOLD of 2 to avoid dehydrating a hook incorrectly.
2160
- if (isHooksPath && path[path.length - 1] === 'subHooks') {
2161
- return true;
2159
+ if (isHooksPath) {
2160
+ if (path.length === 1) {
2161
+ // Never dehydrate the hooks object at the top level.
2162
+ return true;
2163
+ }
2164
+ if (
2165
+ path[path.length - 1] === 'subHooks' ||
2166
+ path[path.length - 2] === 'subHooks'
2167
+ ) {
2168
+ // Never dehydrate the subHooks array
2169
+ return true;
2170
+ }
2171
}
2172
2173
let current =
src/hydration.js
-3
@@ -41,9 +41,6 @@ type Dehydrated = {|
41
//
42
// Reducing this threshold will improve the speed of initial component inspection,
43
// but may decrease the responsiveness of expanding objects/arrays to inspect further.
44
-//
45
-// Note that reducing the threshold to below 2 effectively breaks the inspected hooks interface.
46
-// It is only safe to dehydrate hooks within the "value" key, never within the "subHooks" array directly.
44
const LEVEL_THRESHOLD = 2;
45
46
/**