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 &&

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

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,9 +2593,15 @@ function renderWithHooksAgain(workInProgress, Component, props, secondArg) {
25932593
if (25 <= numberOfReRenders) throw Error(formatProdErrorMessage(301));
25942594
numberOfReRenders += 1;
25952595
workInProgressHook = currentHook = null;
2596-
workInProgress.updateQueue = null;
2596+
if (null != workInProgress.updateQueue) {
2597+
var children = workInProgress.updateQueue;
2598+
children.lastEffect = null;
2599+
children.events = null;
2600+
children.stores = null;
2601+
null != children.memoCache && (children.memoCache.index = 0);
2602+
}
25972603
ReactSharedInternals.H = HooksDispatcherOnRerender;
2598-
var children = Component(props, secondArg);
2604+
children = Component(props, secondArg);
25992605
} while (didScheduleRenderPhaseUpdateDuringThisPass);
26002606
return children;
26012607
}
@@ -3203,17 +3209,16 @@ function rerenderActionState(action) {
32033209
function pushEffect(tag, create, inst, deps) {
32043210
tag = { tag: tag, create: create, inst: inst, deps: deps, next: null };
32053211
create = currentlyRenderingFiber$1.updateQueue;
3206-
null === create
3207-
? ((create = createFunctionComponentUpdateQueue()),
3208-
(currentlyRenderingFiber$1.updateQueue = create),
3209-
(create.lastEffect = tag.next = tag))
3210-
: ((inst = create.lastEffect),
3211-
null === inst
3212-
? (create.lastEffect = tag.next = tag)
3213-
: ((deps = inst.next),
3214-
(inst.next = tag),
3215-
(tag.next = deps),
3216-
(create.lastEffect = tag)));
3212+
null === create &&
3213+
((create = createFunctionComponentUpdateQueue()),
3214+
(currentlyRenderingFiber$1.updateQueue = create));
3215+
inst = create.lastEffect;
3216+
null === inst
3217+
? (create.lastEffect = tag.next = tag)
3218+
: ((deps = inst.next),
3219+
(inst.next = tag),
3220+
(tag.next = deps),
3221+
(create.lastEffect = tag));
32173222
return tag;
32183223
}
32193224
function updateRef() {
@@ -4683,6 +4688,7 @@ function replayFunctionComponent(
46834688
renderLanes
46844689
) {
46854690
prepareToReadContext(workInProgress);
4691+
workInProgress.updateQueue = null;
46864692
nextProps = renderWithHooksAgain(
46874693
workInProgress,
46884694
Component,
@@ -10773,27 +10779,27 @@ var slice = Array.prototype.slice,
1077310779
};
1077410780
return Text;
1077510781
})(React.Component);
10776-
var internals$jscomp$inline_1420 = {
10782+
var internals$jscomp$inline_1422 = {
1077710783
bundleType: 0,
10778-
version: "19.0.0-www-classic-a03254bc-20240905",
10784+
version: "19.0.0-www-classic-727b3615-20240906",
1077910785
rendererPackageName: "react-art",
1078010786
currentDispatcherRef: ReactSharedInternals,
1078110787
findFiberByHostInstance: function () {
1078210788
return null;
1078310789
},
10784-
reconcilerVersion: "19.0.0-www-classic-a03254bc-20240905"
10790+
reconcilerVersion: "19.0.0-www-classic-727b3615-20240906"
1078510791
};
1078610792
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
10787-
var hook$jscomp$inline_1421 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
10793+
var hook$jscomp$inline_1423 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
1078810794
if (
10789-
!hook$jscomp$inline_1421.isDisabled &&
10790-
hook$jscomp$inline_1421.supportsFiber
10795+
!hook$jscomp$inline_1423.isDisabled &&
10796+
hook$jscomp$inline_1423.supportsFiber
1079110797
)
1079210798
try {
10793-
(rendererID = hook$jscomp$inline_1421.inject(
10794-
internals$jscomp$inline_1420
10799+
(rendererID = hook$jscomp$inline_1423.inject(
10800+
internals$jscomp$inline_1422
1079510801
)),
10796-
(injectedHook = hook$jscomp$inline_1421);
10802+
(injectedHook = hook$jscomp$inline_1423);
1079710803
} catch (err) {}
1079810804
}
1079910805
var Path = Mode$1.Path;
@@ -10807,4 +10813,4 @@ exports.RadialGradient = RadialGradient;
1080710813
exports.Shape = TYPES.SHAPE;
1080810814
exports.Surface = Surface;
1080910815
exports.Text = Text;
10810-
exports.version = "19.0.0-www-classic-a03254bc-20240905";
10816+
exports.version = "19.0.0-www-classic-727b3615-20240906";

0 commit comments

Comments
 (0)