Skip to content

Commit eb67bf1

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 [727b361](727b361)
1 parent 716cd41 commit eb67bf1

34 files changed

+1346
-1226
lines changed

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a03254bc60b06c535c37e43c53b1fd40757b2ef4
1+
727b3615287074ddaa28069bfbd2dfee8cf73575
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a03254bc60b06c535c37e43c53b1fd40757b2ef4
1+
727b3615287074ddaa28069bfbd2dfee8cf73575

compiled/facebook-www/React-dev.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,7 @@ __DEV__ &&
20012001
exports.useTransition = function () {
20022002
return resolveDispatcher().useTransition();
20032003
};
2004-
exports.version = "19.0.0-www-classic-a03254bc-20240905";
2004+
exports.version = "19.0.0-www-classic-727b3615-20240906";
20052005
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
20062006
"function" ===
20072007
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-dev.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,7 @@ __DEV__ &&
19811981
exports.useTransition = function () {
19821982
return resolveDispatcher().useTransition();
19831983
};
1984-
exports.version = "19.0.0-www-modern-a03254bc-20240905";
1984+
exports.version = "19.0.0-www-modern-727b3615-20240906";
19851985
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
19861986
"function" ===
19871987
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-prod.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,4 +665,4 @@ exports.useSyncExternalStore = function (
665665
exports.useTransition = function () {
666666
return ReactSharedInternals.H.useTransition();
667667
};
668-
exports.version = "19.0.0-www-classic-a03254bc-20240905";
668+
exports.version = "19.0.0-www-classic-727b3615-20240906";

compiled/facebook-www/React-prod.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,4 +665,4 @@ exports.useSyncExternalStore = function (
665665
exports.useTransition = function () {
666666
return ReactSharedInternals.H.useTransition();
667667
};
668-
exports.version = "19.0.0-www-modern-a03254bc-20240905";
668+
exports.version = "19.0.0-www-modern-727b3615-20240906";

compiled/facebook-www/React-profiling.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ exports.useSyncExternalStore = function (
669669
exports.useTransition = function () {
670670
return ReactSharedInternals.H.useTransition();
671671
};
672-
exports.version = "19.0.0-www-classic-a03254bc-20240905";
672+
exports.version = "19.0.0-www-classic-727b3615-20240906";
673673
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
674674
"function" ===
675675
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-profiling.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ exports.useSyncExternalStore = function (
669669
exports.useTransition = function () {
670670
return ReactSharedInternals.H.useTransition();
671671
};
672-
exports.version = "19.0.0-www-modern-a03254bc-20240905";
672+
exports.version = "19.0.0-www-modern-727b3615-20240906";
673673
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
674674
"function" ===
675675
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/ReactART-dev.classic.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4096,10 +4096,16 @@ __DEV__ &&
40964096
numberOfReRenders += 1;
40974097
ignorePreviousDependencies = !1;
40984098
workInProgressHook = currentHook = null;
4099-
workInProgress.updateQueue = null;
4099+
if (null != workInProgress.updateQueue) {
4100+
var children = workInProgress.updateQueue;
4101+
children.lastEffect = null;
4102+
children.events = null;
4103+
children.stores = null;
4104+
null != children.memoCache && (children.memoCache.index = 0);
4105+
}
41004106
hookTypesUpdateIndexDev = -1;
41014107
ReactSharedInternals.H = HooksDispatcherOnRerenderInDEV;
4102-
var children = callComponentInDEV(Component, props, secondArg);
4108+
children = callComponentInDEV(Component, props, secondArg);
41034109
} while (didScheduleRenderPhaseUpdateDuringThisPass);
41044110
return children;
41054111
}
@@ -4878,17 +4884,16 @@ __DEV__ &&
48784884
function pushEffect(tag, create, inst, deps) {
48794885
tag = { tag: tag, create: create, inst: inst, deps: deps, next: null };
48804886
create = currentlyRenderingFiber$1.updateQueue;
4881-
null === create
4882-
? ((create = createFunctionComponentUpdateQueue()),
4883-
(currentlyRenderingFiber$1.updateQueue = create),
4884-
(create.lastEffect = tag.next = tag))
4885-
: ((inst = create.lastEffect),
4886-
null === inst
4887-
? (create.lastEffect = tag.next = tag)
4888-
: ((deps = inst.next),
4889-
(inst.next = tag),
4890-
(tag.next = deps),
4891-
(create.lastEffect = tag)));
4887+
null === create &&
4888+
((create = createFunctionComponentUpdateQueue()),
4889+
(currentlyRenderingFiber$1.updateQueue = create));
4890+
inst = create.lastEffect;
4891+
null === inst
4892+
? (create.lastEffect = tag.next = tag)
4893+
: ((deps = inst.next),
4894+
(inst.next = tag),
4895+
(tag.next = deps),
4896+
(create.lastEffect = tag));
48924897
return tag;
48934898
}
48944899
function mountRef(initialValue) {
@@ -6691,6 +6696,7 @@ __DEV__ &&
66916696
hookTypesUpdateIndexDev = -1;
66926697
ignorePreviousDependencies =
66936698
null !== current && current.type !== workInProgress.type;
6699+
workInProgress.updateQueue = null;
66946700
nextProps = renderWithHooksAgain(
66956701
workInProgress,
66966702
Component,
@@ -16958,11 +16964,11 @@ __DEV__ &&
1695816964
(function () {
1695916965
var internals = {
1696016966
bundleType: 1,
16961-
version: "19.0.0-www-classic-a03254bc-20240905",
16967+
version: "19.0.0-www-classic-727b3615-20240906",
1696216968
rendererPackageName: "react-art",
1696316969
currentDispatcherRef: ReactSharedInternals,
1696416970
findFiberByHostInstance: getInstanceFromNode,
16965-
reconcilerVersion: "19.0.0-www-classic-a03254bc-20240905"
16971+
reconcilerVersion: "19.0.0-www-classic-727b3615-20240906"
1696616972
};
1696716973
internals.overrideHookState = overrideHookState;
1696816974
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -16996,7 +17002,7 @@ __DEV__ &&
1699617002
exports.Shape = Shape;
1699717003
exports.Surface = Surface;
1699817004
exports.Text = Text;
16999-
exports.version = "19.0.0-www-classic-a03254bc-20240905";
17005+
exports.version = "19.0.0-www-classic-727b3615-20240906";
1700017006
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
1700117007
"function" ===
1700217008
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/ReactART-dev.modern.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3972,10 +3972,16 @@ __DEV__ &&
39723972
numberOfReRenders += 1;
39733973
ignorePreviousDependencies = !1;
39743974
workInProgressHook = currentHook = null;
3975-
workInProgress.updateQueue = null;
3975+
if (null != workInProgress.updateQueue) {
3976+
var children = workInProgress.updateQueue;
3977+
children.lastEffect = null;
3978+
children.events = null;
3979+
children.stores = null;
3980+
null != children.memoCache && (children.memoCache.index = 0);
3981+
}
39763982
hookTypesUpdateIndexDev = -1;
39773983
ReactSharedInternals.H = HooksDispatcherOnRerenderInDEV;
3978-
var children = callComponentInDEV(Component, props, secondArg);
3984+
children = callComponentInDEV(Component, props, secondArg);
39793985
} while (didScheduleRenderPhaseUpdateDuringThisPass);
39803986
return children;
39813987
}
@@ -4754,17 +4760,16 @@ __DEV__ &&
47544760
function pushEffect(tag, create, inst, deps) {
47554761
tag = { tag: tag, create: create, inst: inst, deps: deps, next: null };
47564762
create = currentlyRenderingFiber$1.updateQueue;
4757-
null === create
4758-
? ((create = createFunctionComponentUpdateQueue()),
4759-
(currentlyRenderingFiber$1.updateQueue = create),
4760-
(create.lastEffect = tag.next = tag))
4761-
: ((inst = create.lastEffect),
4762-
null === inst
4763-
? (create.lastEffect = tag.next = tag)
4764-
: ((deps = inst.next),
4765-
(inst.next = tag),
4766-
(tag.next = deps),
4767-
(create.lastEffect = tag)));
4763+
null === create &&
4764+
((create = createFunctionComponentUpdateQueue()),
4765+
(currentlyRenderingFiber$1.updateQueue = create));
4766+
inst = create.lastEffect;
4767+
null === inst
4768+
? (create.lastEffect = tag.next = tag)
4769+
: ((deps = inst.next),
4770+
(inst.next = tag),
4771+
(tag.next = deps),
4772+
(create.lastEffect = tag));
47684773
return tag;
47694774
}
47704775
function mountRef(initialValue) {
@@ -6203,6 +6208,7 @@ __DEV__ &&
62036208
hookTypesUpdateIndexDev = -1;
62046209
ignorePreviousDependencies =
62056210
null !== current && current.type !== workInProgress.type;
6211+
workInProgress.updateQueue = null;
62066212
nextProps = renderWithHooksAgain(
62076213
workInProgress,
62086214
Component,
@@ -16404,11 +16410,11 @@ __DEV__ &&
1640416410
(function () {
1640516411
var internals = {
1640616412
bundleType: 1,
16407-
version: "19.0.0-www-modern-a03254bc-20240905",
16413+
version: "19.0.0-www-modern-727b3615-20240906",
1640816414
rendererPackageName: "react-art",
1640916415
currentDispatcherRef: ReactSharedInternals,
1641016416
findFiberByHostInstance: getInstanceFromNode,
16411-
reconcilerVersion: "19.0.0-www-modern-a03254bc-20240905"
16417+
reconcilerVersion: "19.0.0-www-modern-727b3615-20240906"
1641216418
};
1641316419
internals.overrideHookState = overrideHookState;
1641416420
internals.overrideHookStateDeletePath = overrideHookStateDeletePath;
@@ -16442,7 +16448,7 @@ __DEV__ &&
1644216448
exports.Shape = Shape;
1644316449
exports.Surface = Surface;
1644416450
exports.Text = Text;
16445-
exports.version = "19.0.0-www-modern-a03254bc-20240905";
16451+
exports.version = "19.0.0-www-modern-727b3615-20240906";
1644616452
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
1644716453
"function" ===
1644816454
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)