Skip to content

Commit 1589271

Browse files
committed
Fix useMemoCache with setState in render
Fixes the bug that @alexmckenley and @mofeiZ found where setState-in-render can reset useMemoCache and cause an infinite loop. The bug was that renderWithHooksAgain() was not resetting hook state when rerendering (so useMemo values were preserved) but was resetting the updateQueue. This meant that the entire memo cache was cleared on a setState-in-render. The fix here is to call a new helper function to clear the update queue. It nulls out other properties, but for memoCache it just sets the index back to zero. ghstack-source-id: fc0947c Pull Request resolved: #30889 DiffTrain build for commit 727b361.
1 parent 6f1fca9 commit 1589271

File tree

14 files changed

+267
-213
lines changed

14 files changed

+267
-213
lines changed

compiled-rn/VERSION_NATIVE_FB

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19.0.0-native-fb-a03254bc-20240905
1+
19.0.0-native-fb-727b3615-20240906

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/react-test-renderer/cjs/ReactTestRenderer-dev.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
* @noflow
88
* @nolint
99
* @preventMunge
10-
* @generated SignedSource<<db06c53f5b9d3c1119784dde6f94f0de>>
10+
* @generated SignedSource<<7986e32cbf9ce62a80c6cdb99f68f4d4>>
1111
*/
1212

1313
"use strict";
1414
__DEV__ &&
1515
(function () {
16-
function JSCompiler_object_inline_createNodeMock_1101() {
16+
function JSCompiler_object_inline_createNodeMock_1103() {
1717
return null;
1818
}
1919
function findHook(fiber, id) {
@@ -3776,10 +3776,16 @@ __DEV__ &&
37763776
numberOfReRenders += 1;
37773777
ignorePreviousDependencies = !1;
37783778
workInProgressHook = currentHook = null;
3779-
workInProgress.updateQueue = null;
3779+
if (null != workInProgress.updateQueue) {
3780+
var children = workInProgress.updateQueue;
3781+
children.lastEffect = null;
3782+
children.events = null;
3783+
children.stores = null;
3784+
null != children.memoCache && (children.memoCache.index = 0);
3785+
}
37803786
hookTypesUpdateIndexDev = -1;
37813787
ReactSharedInternals.H = HooksDispatcherOnRerenderInDEV;
3782-
var children = callComponentInDEV(Component, props, secondArg);
3788+
children = callComponentInDEV(Component, props, secondArg);
37833789
} while (didScheduleRenderPhaseUpdateDuringThisPass);
37843790
return children;
37853791
}
@@ -4524,17 +4530,16 @@ __DEV__ &&
45244530
function pushEffect(tag, create, inst, deps) {
45254531
tag = { tag: tag, create: create, inst: inst, deps: deps, next: null };
45264532
create = currentlyRenderingFiber$1.updateQueue;
4527-
null === create
4528-
? ((create = createFunctionComponentUpdateQueue()),
4529-
(currentlyRenderingFiber$1.updateQueue = create),
4530-
(create.lastEffect = tag.next = tag))
4531-
: ((inst = create.lastEffect),
4532-
null === inst
4533-
? (create.lastEffect = tag.next = tag)
4534-
: ((deps = inst.next),
4535-
(inst.next = tag),
4536-
(tag.next = deps),
4537-
(create.lastEffect = tag)));
4533+
null === create &&
4534+
((create = createFunctionComponentUpdateQueue()),
4535+
(currentlyRenderingFiber$1.updateQueue = create));
4536+
inst = create.lastEffect;
4537+
null === inst
4538+
? (create.lastEffect = tag.next = tag)
4539+
: ((deps = inst.next),
4540+
(inst.next = tag),
4541+
(tag.next = deps),
4542+
(create.lastEffect = tag));
45384543
return tag;
45394544
}
45404545
function mountRef(initialValue) {
@@ -6101,6 +6106,7 @@ __DEV__ &&
61016106
hookTypesUpdateIndexDev = -1;
61026107
ignorePreviousDependencies =
61036108
null !== current && current.type !== workInProgress.type;
6109+
workInProgress.updateQueue = null;
61046110
nextProps = renderWithHooksAgain(
61056111
workInProgress,
61066112
Component,
@@ -15051,11 +15057,11 @@ __DEV__ &&
1505115057
(function () {
1505215058
var internals = {
1505315059
bundleType: 1,
15054-
version: "19.0.0-native-fb-a03254bc-20240905",
15060+
version: "19.0.0-native-fb-727b3615-20240906",
1505515061
rendererPackageName: "react-test-renderer",
1505615062
currentDispatcherRef: ReactSharedInternals,
1505715063
findFiberByHostInstance: getInstanceFromNode,
15058-
reconcilerVersion: "19.0.0-native-fb-a03254bc-20240905"
15064+
reconcilerVersion: "19.0.0-native-fb-727b3615-20240906"
1505915065
};
1506015066
internals.overrideHookState = overrideHookState;
1506115067
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -15077,7 +15083,7 @@ __DEV__ &&
1507715083
exports._Scheduler = Scheduler;
1507815084
exports.act = act;
1507915085
exports.create = function (element, options) {
15080-
var createNodeMock = JSCompiler_object_inline_createNodeMock_1101,
15086+
var createNodeMock = JSCompiler_object_inline_createNodeMock_1103,
1508115087
isConcurrent = !1,
1508215088
isStrictMode = !1;
1508315089
"object" === typeof options &&
@@ -15200,5 +15206,5 @@ __DEV__ &&
1520015206
flushSyncWorkAcrossRoots_impl(0, !0));
1520115207
}
1520215208
};
15203-
exports.version = "19.0.0-native-fb-a03254bc-20240905";
15209+
exports.version = "19.0.0-native-fb-727b3615-20240906";
1520415210
})();

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/react-test-renderer/cjs/ReactTestRenderer-prod.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @noflow
88
* @nolint
99
* @preventMunge
10-
* @generated SignedSource<<4fca6319224428deafef3fdf57cb682f>>
10+
* @generated SignedSource<<737b0c6df35b54b9ca58121c9993e75b>>
1111
*/
1212

1313
"use strict";
@@ -2406,9 +2406,15 @@ function renderWithHooksAgain(workInProgress, Component, props, secondArg) {
24062406
);
24072407
numberOfReRenders += 1;
24082408
workInProgressHook = currentHook = null;
2409-
workInProgress.updateQueue = null;
2409+
if (null != workInProgress.updateQueue) {
2410+
var children = workInProgress.updateQueue;
2411+
children.lastEffect = null;
2412+
children.events = null;
2413+
children.stores = null;
2414+
null != children.memoCache && (children.memoCache.index = 0);
2415+
}
24102416
ReactSharedInternals.H = HooksDispatcherOnRerender;
2411-
var children = Component(props, secondArg);
2417+
children = Component(props, secondArg);
24122418
} while (didScheduleRenderPhaseUpdateDuringThisPass);
24132419
return children;
24142420
}
@@ -2997,17 +3003,16 @@ function rerenderActionState(action) {
29973003
function pushEffect(tag, create, inst, deps) {
29983004
tag = { tag: tag, create: create, inst: inst, deps: deps, next: null };
29993005
create = currentlyRenderingFiber$1.updateQueue;
3000-
null === create
3001-
? ((create = createFunctionComponentUpdateQueue()),
3002-
(currentlyRenderingFiber$1.updateQueue = create),
3003-
(create.lastEffect = tag.next = tag))
3004-
: ((inst = create.lastEffect),
3005-
null === inst
3006-
? (create.lastEffect = tag.next = tag)
3007-
: ((deps = inst.next),
3008-
(inst.next = tag),
3009-
(tag.next = deps),
3010-
(create.lastEffect = tag)));
3006+
null === create &&
3007+
((create = createFunctionComponentUpdateQueue()),
3008+
(currentlyRenderingFiber$1.updateQueue = create));
3009+
inst = create.lastEffect;
3010+
null === inst
3011+
? (create.lastEffect = tag.next = tag)
3012+
: ((deps = inst.next),
3013+
(inst.next = tag),
3014+
(tag.next = deps),
3015+
(create.lastEffect = tag));
30113016
return tag;
30123017
}
30133018
function updateRef() {
@@ -4228,6 +4233,7 @@ function replayFunctionComponent(
42284233
renderLanes
42294234
) {
42304235
prepareToReadContext(workInProgress, renderLanes);
4236+
workInProgress.updateQueue = null;
42314237
nextProps = renderWithHooksAgain(
42324238
workInProgress,
42334239
Component,
@@ -9437,28 +9443,28 @@ function wrapFiber(fiber) {
94379443
fiberToWrapper.set(fiber, wrapper));
94389444
return wrapper;
94399445
}
9440-
var internals$jscomp$inline_1262 = {
9446+
var internals$jscomp$inline_1264 = {
94419447
bundleType: 0,
9442-
version: "19.0.0-native-fb-a03254bc-20240905",
9448+
version: "19.0.0-native-fb-727b3615-20240906",
94439449
rendererPackageName: "react-test-renderer",
94449450
currentDispatcherRef: ReactSharedInternals,
94459451
findFiberByHostInstance: function (mockNode) {
94469452
mockNode = nodeToInstanceMap.get(mockNode);
94479453
return void 0 !== mockNode ? mockNode.internalInstanceHandle : null;
94489454
},
9449-
reconcilerVersion: "19.0.0-native-fb-a03254bc-20240905"
9455+
reconcilerVersion: "19.0.0-native-fb-727b3615-20240906"
94509456
};
94519457
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
9452-
var hook$jscomp$inline_1263 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
9458+
var hook$jscomp$inline_1265 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
94539459
if (
9454-
!hook$jscomp$inline_1263.isDisabled &&
9455-
hook$jscomp$inline_1263.supportsFiber
9460+
!hook$jscomp$inline_1265.isDisabled &&
9461+
hook$jscomp$inline_1265.supportsFiber
94569462
)
94579463
try {
9458-
(rendererID = hook$jscomp$inline_1263.inject(
9459-
internals$jscomp$inline_1262
9464+
(rendererID = hook$jscomp$inline_1265.inject(
9465+
internals$jscomp$inline_1264
94609466
)),
9461-
(injectedHook = hook$jscomp$inline_1263);
9467+
(injectedHook = hook$jscomp$inline_1265);
94629468
} catch (err) {}
94639469
}
94649470
exports._Scheduler = Scheduler;
@@ -9582,4 +9588,4 @@ exports.unstable_batchedUpdates = function (fn, a) {
95829588
flushSyncWorkAcrossRoots_impl(0, !0));
95839589
}
95849590
};
9585-
exports.version = "19.0.0-native-fb-a03254bc-20240905";
9591+
exports.version = "19.0.0-native-fb-727b3615-20240906";

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/react-test-renderer/cjs/ReactTestRenderer-profiling.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @noflow
88
* @nolint
99
* @preventMunge
10-
* @generated SignedSource<<7b62c4c79e4f3fff37a3237b451e7316>>
10+
* @generated SignedSource<<810ece3813f16a4de75fcacac7d7813e>>
1111
*/
1212

1313
"use strict";
@@ -2483,9 +2483,15 @@ function renderWithHooksAgain(workInProgress, Component, props, secondArg) {
24832483
);
24842484
numberOfReRenders += 1;
24852485
workInProgressHook = currentHook = null;
2486-
workInProgress.updateQueue = null;
2486+
if (null != workInProgress.updateQueue) {
2487+
var children = workInProgress.updateQueue;
2488+
children.lastEffect = null;
2489+
children.events = null;
2490+
children.stores = null;
2491+
null != children.memoCache && (children.memoCache.index = 0);
2492+
}
24872493
ReactSharedInternals.H = HooksDispatcherOnRerender;
2488-
var children = Component(props, secondArg);
2494+
children = Component(props, secondArg);
24892495
} while (didScheduleRenderPhaseUpdateDuringThisPass);
24902496
return children;
24912497
}
@@ -3074,17 +3080,16 @@ function rerenderActionState(action) {
30743080
function pushEffect(tag, create, inst, deps) {
30753081
tag = { tag: tag, create: create, inst: inst, deps: deps, next: null };
30763082
create = currentlyRenderingFiber$1.updateQueue;
3077-
null === create
3078-
? ((create = createFunctionComponentUpdateQueue()),
3079-
(currentlyRenderingFiber$1.updateQueue = create),
3080-
(create.lastEffect = tag.next = tag))
3081-
: ((inst = create.lastEffect),
3082-
null === inst
3083-
? (create.lastEffect = tag.next = tag)
3084-
: ((deps = inst.next),
3085-
(inst.next = tag),
3086-
(tag.next = deps),
3087-
(create.lastEffect = tag)));
3083+
null === create &&
3084+
((create = createFunctionComponentUpdateQueue()),
3085+
(currentlyRenderingFiber$1.updateQueue = create));
3086+
inst = create.lastEffect;
3087+
null === inst
3088+
? (create.lastEffect = tag.next = tag)
3089+
: ((deps = inst.next),
3090+
(inst.next = tag),
3091+
(tag.next = deps),
3092+
(create.lastEffect = tag));
30883093
return tag;
30893094
}
30903095
function updateRef() {
@@ -4380,6 +4385,7 @@ function replayFunctionComponent(
43804385
) {
43814386
prepareToReadContext(workInProgress, renderLanes);
43824387
markComponentRenderStarted(workInProgress);
4388+
workInProgress.updateQueue = null;
43834389
nextProps = renderWithHooksAgain(
43844390
workInProgress,
43854391
Component,
@@ -10025,16 +10031,16 @@ function wrapFiber(fiber) {
1002510031
fiberToWrapper.set(fiber, wrapper));
1002610032
return wrapper;
1002710033
}
10028-
var internals$jscomp$inline_1137 = {
10034+
var internals$jscomp$inline_1139 = {
1002910035
bundleType: 0,
10030-
version: "19.0.0-native-fb-a03254bc-20240905",
10036+
version: "19.0.0-native-fb-727b3615-20240906",
1003110037
rendererPackageName: "react-test-renderer",
1003210038
currentDispatcherRef: ReactSharedInternals,
1003310039
findFiberByHostInstance: function (mockNode) {
1003410040
mockNode = nodeToInstanceMap.get(mockNode);
1003510041
return void 0 !== mockNode ? mockNode.internalInstanceHandle : null;
1003610042
},
10037-
reconcilerVersion: "19.0.0-native-fb-a03254bc-20240905",
10043+
reconcilerVersion: "19.0.0-native-fb-727b3615-20240906",
1003810044
getLaneLabelMap: function () {
1003910045
for (
1004010046
var map = new Map(), lane = 1, index$138 = 0;
@@ -10052,16 +10058,16 @@ var internals$jscomp$inline_1137 = {
1005210058
}
1005310059
};
1005410060
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
10055-
var hook$jscomp$inline_1339 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
10061+
var hook$jscomp$inline_1341 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
1005610062
if (
10057-
!hook$jscomp$inline_1339.isDisabled &&
10058-
hook$jscomp$inline_1339.supportsFiber
10063+
!hook$jscomp$inline_1341.isDisabled &&
10064+
hook$jscomp$inline_1341.supportsFiber
1005910065
)
1006010066
try {
10061-
(rendererID = hook$jscomp$inline_1339.inject(
10062-
internals$jscomp$inline_1137
10067+
(rendererID = hook$jscomp$inline_1341.inject(
10068+
internals$jscomp$inline_1139
1006310069
)),
10064-
(injectedHook = hook$jscomp$inline_1339);
10070+
(injectedHook = hook$jscomp$inline_1341);
1006510071
} catch (err) {}
1006610072
}
1006710073
exports._Scheduler = Scheduler;
@@ -10185,4 +10191,4 @@ exports.unstable_batchedUpdates = function (fn, a) {
1018510191
flushSyncWorkAcrossRoots_impl(0, !0));
1018610192
}
1018710193
};
10188-
exports.version = "19.0.0-native-fb-a03254bc-20240905";
10194+
exports.version = "19.0.0-native-fb-727b3615-20240906";

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/react/cjs/React-dev.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @noflow
88
* @nolint
99
* @preventMunge
10-
* @generated SignedSource<<de71d97ca0a0d081df978e785a257152>>
10+
* @generated SignedSource<<7891b4d6325e775d1f22d4b9936dd94c>>
1111
*/
1212

1313
"use strict";
@@ -1705,7 +1705,7 @@ __DEV__ &&
17051705
exports.useTransition = function () {
17061706
return resolveDispatcher().useTransition();
17071707
};
1708-
exports.version = "19.0.0-native-fb-a03254bc-20240905";
1708+
exports.version = "19.0.0-native-fb-727b3615-20240906";
17091709
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
17101710
"function" ===
17111711
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/react/cjs/React-prod.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @noflow
88
* @nolint
99
* @preventMunge
10-
* @generated SignedSource<<fd92e608e49eef573fe41656bc76d3f9>>
10+
* @generated SignedSource<<f3fc58abefc1049be1a977fc1f9f00a3>>
1111
*/
1212

1313
"use strict";
@@ -580,4 +580,4 @@ exports.useSyncExternalStore = function (
580580
exports.useTransition = function () {
581581
return ReactSharedInternals.H.useTransition();
582582
};
583-
exports.version = "19.0.0-native-fb-a03254bc-20240905";
583+
exports.version = "19.0.0-native-fb-727b3615-20240906";

compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/react/cjs/React-profiling.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @noflow
88
* @nolint
99
* @preventMunge
10-
* @generated SignedSource<<c130a544f4cf18b91cefd407e6daf6b3>>
10+
* @generated SignedSource<<9807006935b9da557abb42575ba96728>>
1111
*/
1212

1313
"use strict";
@@ -584,7 +584,7 @@ exports.useSyncExternalStore = function (
584584
exports.useTransition = function () {
585585
return ReactSharedInternals.H.useTransition();
586586
};
587-
exports.version = "19.0.0-native-fb-a03254bc-20240905";
587+
exports.version = "19.0.0-native-fb-727b3615-20240906";
588588
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
589589
"function" ===
590590
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a03254bc60b06c535c37e43c53b1fd40757b2ef4
1+
727b3615287074ddaa28069bfbd2dfee8cf73575

0 commit comments

Comments
 (0)