Skip to content

Commit a52f427

Browse files
committed
Ensure useState and useReducer initializer functions are double invoked in StrictMode (#28248)
DiffTrain build for [97fd3e7](97fd3e7)
1 parent 47b1b75 commit a52f427

19 files changed

+297
-81
lines changed

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
db120f69ec7a0b8c7f38ca7a1ddb1886de92e465
1+
97fd3e7064b162f05b1bac3962ed10c6559c346c

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,4 @@ exports.useSyncExternalStore = function (
570570
exports.useTransition = function () {
571571
return ReactCurrentDispatcher.current.useTransition();
572572
};
573-
exports.version = "18.3.0-www-classic-19d42c66";
573+
exports.version = "18.3.0-www-classic-f4740bf5";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ exports.useSyncExternalStore = function (
574574
exports.useTransition = function () {
575575
return ReactCurrentDispatcher.current.useTransition();
576576
};
577-
exports.version = "18.3.0-www-classic-75f5108a";
577+
exports.version = "18.3.0-www-classic-b75b6a09";
578578
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
579579
"function" ===
580580
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ if (__DEV__) {
6666
return self;
6767
}
6868

69-
var ReactVersion = "18.3.0-www-classic-d2bd5964";
69+
var ReactVersion = "18.3.0-www-classic-6e4380f3";
7070

7171
var LegacyRoot = 0;
7272
var ConcurrentRoot = 1;
@@ -9070,6 +9070,12 @@ if (__DEV__) {
90709070

90719071
if (init !== undefined) {
90729072
initialState = init(initialArg);
9073+
9074+
if (shouldDoubleInvokeUserFnsInHooksDEV) {
9075+
setIsStrictModeForDevtools(true);
9076+
init(initialArg);
9077+
setIsStrictModeForDevtools(false);
9078+
}
90739079
} else {
90749080
initialState = initialArg;
90759081
}
@@ -9619,8 +9625,16 @@ if (__DEV__) {
96199625
var hook = mountWorkInProgressHook();
96209626

96219627
if (typeof initialState === "function") {
9622-
// $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
9623-
initialState = initialState();
9628+
var initialStateInitializer = initialState; // $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
9629+
9630+
initialState = initialStateInitializer();
9631+
9632+
if (shouldDoubleInvokeUserFnsInHooksDEV) {
9633+
setIsStrictModeForDevtools(true); // $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
9634+
9635+
initialStateInitializer();
9636+
setIsStrictModeForDevtools(false);
9637+
}
96249638
}
96259639

96269640
hook.memoizedState = hook.baseState = initialState;

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ if (__DEV__) {
6666
return self;
6767
}
6868

69-
var ReactVersion = "18.3.0-www-modern-3985e4ea";
69+
var ReactVersion = "18.3.0-www-modern-46693fec";
7070

7171
var LegacyRoot = 0;
7272
var ConcurrentRoot = 1;
@@ -8820,6 +8820,12 @@ if (__DEV__) {
88208820

88218821
if (init !== undefined) {
88228822
initialState = init(initialArg);
8823+
8824+
if (shouldDoubleInvokeUserFnsInHooksDEV) {
8825+
setIsStrictModeForDevtools(true);
8826+
init(initialArg);
8827+
setIsStrictModeForDevtools(false);
8828+
}
88238829
} else {
88248830
initialState = initialArg;
88258831
}
@@ -9369,8 +9375,16 @@ if (__DEV__) {
93699375
var hook = mountWorkInProgressHook();
93709376

93719377
if (typeof initialState === "function") {
9372-
// $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
9373-
initialState = initialState();
9378+
var initialStateInitializer = initialState; // $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
9379+
9380+
initialState = initialStateInitializer();
9381+
9382+
if (shouldDoubleInvokeUserFnsInHooksDEV) {
9383+
setIsStrictModeForDevtools(true); // $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
9384+
9385+
initialStateInitializer();
9386+
setIsStrictModeForDevtools(false);
9387+
}
93749388
}
93759389

93769390
hook.memoizedState = hook.baseState = initialState;

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,7 +3059,14 @@ function forceStoreRerender(fiber) {
30593059
}
30603060
function mountStateImpl(initialState) {
30613061
var hook = mountWorkInProgressHook();
3062-
"function" === typeof initialState && (initialState = initialState());
3062+
if ("function" === typeof initialState) {
3063+
var initialStateInitializer = initialState;
3064+
initialState = initialStateInitializer();
3065+
shouldDoubleInvokeUserFnsInHooksDEV &&
3066+
(setIsStrictModeForDevtools(!0),
3067+
initialStateInitializer(),
3068+
setIsStrictModeForDevtools(!1));
3069+
}
30633070
hook.memoizedState = hook.baseState = initialState;
30643071
hook.queue = {
30653072
pending: null,
@@ -3645,14 +3652,20 @@ var HooksDispatcherOnMount = {
36453652
},
36463653
useReducer: function (reducer, initialArg, init) {
36473654
var hook = mountWorkInProgressHook();
3648-
initialArg = void 0 !== init ? init(initialArg) : initialArg;
3649-
hook.memoizedState = hook.baseState = initialArg;
3655+
if (void 0 !== init) {
3656+
var initialState = init(initialArg);
3657+
shouldDoubleInvokeUserFnsInHooksDEV &&
3658+
(setIsStrictModeForDevtools(!0),
3659+
init(initialArg),
3660+
setIsStrictModeForDevtools(!1));
3661+
} else initialState = initialArg;
3662+
hook.memoizedState = hook.baseState = initialState;
36503663
reducer = {
36513664
pending: null,
36523665
lanes: 0,
36533666
dispatch: null,
36543667
lastRenderedReducer: reducer,
3655-
lastRenderedState: initialArg
3668+
lastRenderedState: initialState
36563669
};
36573670
hook.queue = reducer;
36583671
reducer = reducer.dispatch = dispatchReducerAction.bind(
@@ -10542,7 +10555,7 @@ var slice = Array.prototype.slice,
1054210555
return null;
1054310556
},
1054410557
bundleType: 0,
10545-
version: "18.3.0-www-classic-776451b9",
10558+
version: "18.3.0-www-classic-8f257f8f",
1054610559
rendererPackageName: "react-art"
1054710560
};
1054810561
var internals$jscomp$inline_1323 = {
@@ -10573,7 +10586,7 @@ var internals$jscomp$inline_1323 = {
1057310586
scheduleRoot: null,
1057410587
setRefreshHandler: null,
1057510588
getCurrentFiber: null,
10576-
reconcilerVersion: "18.3.0-www-classic-776451b9"
10589+
reconcilerVersion: "18.3.0-www-classic-8f257f8f"
1057710590
};
1057810591
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
1057910592
var hook$jscomp$inline_1324 = __REACT_DEVTOOLS_GLOBAL_HOOK__;

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,7 +2866,14 @@ function forceStoreRerender(fiber) {
28662866
}
28672867
function mountStateImpl(initialState) {
28682868
var hook = mountWorkInProgressHook();
2869-
"function" === typeof initialState && (initialState = initialState());
2869+
if ("function" === typeof initialState) {
2870+
var initialStateInitializer = initialState;
2871+
initialState = initialStateInitializer();
2872+
shouldDoubleInvokeUserFnsInHooksDEV &&
2873+
(setIsStrictModeForDevtools(!0),
2874+
initialStateInitializer(),
2875+
setIsStrictModeForDevtools(!1));
2876+
}
28702877
hook.memoizedState = hook.baseState = initialState;
28712878
hook.queue = {
28722879
pending: null,
@@ -3452,14 +3459,20 @@ var HooksDispatcherOnMount = {
34523459
},
34533460
useReducer: function (reducer, initialArg, init) {
34543461
var hook = mountWorkInProgressHook();
3455-
initialArg = void 0 !== init ? init(initialArg) : initialArg;
3456-
hook.memoizedState = hook.baseState = initialArg;
3462+
if (void 0 !== init) {
3463+
var initialState = init(initialArg);
3464+
shouldDoubleInvokeUserFnsInHooksDEV &&
3465+
(setIsStrictModeForDevtools(!0),
3466+
init(initialArg),
3467+
setIsStrictModeForDevtools(!1));
3468+
} else initialState = initialArg;
3469+
hook.memoizedState = hook.baseState = initialState;
34573470
reducer = {
34583471
pending: null,
34593472
lanes: 0,
34603473
dispatch: null,
34613474
lastRenderedReducer: reducer,
3462-
lastRenderedState: initialArg
3475+
lastRenderedState: initialState
34633476
};
34643477
hook.queue = reducer;
34653478
reducer = reducer.dispatch = dispatchReducerAction.bind(
@@ -10208,7 +10221,7 @@ var slice = Array.prototype.slice,
1020810221
return null;
1020910222
},
1021010223
bundleType: 0,
10211-
version: "18.3.0-www-modern-aa622b11",
10224+
version: "18.3.0-www-modern-371634b7",
1021210225
rendererPackageName: "react-art"
1021310226
};
1021410227
var internals$jscomp$inline_1303 = {
@@ -10239,7 +10252,7 @@ var internals$jscomp$inline_1303 = {
1023910252
scheduleRoot: null,
1024010253
setRefreshHandler: null,
1024110254
getCurrentFiber: null,
10242-
reconcilerVersion: "18.3.0-www-modern-aa622b11"
10255+
reconcilerVersion: "18.3.0-www-modern-371634b7"
1024310256
};
1024410257
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
1024510258
var hook$jscomp$inline_1304 = __REACT_DEVTOOLS_GLOBAL_HOOK__;

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13760,6 +13760,12 @@ if (__DEV__) {
1376013760

1376113761
if (init !== undefined) {
1376213762
initialState = init(initialArg);
13763+
13764+
if (shouldDoubleInvokeUserFnsInHooksDEV) {
13765+
setIsStrictModeForDevtools(true);
13766+
init(initialArg);
13767+
setIsStrictModeForDevtools(false);
13768+
}
1376313769
} else {
1376413770
initialState = initialArg;
1376513771
}
@@ -14342,8 +14348,16 @@ if (__DEV__) {
1434214348
var hook = mountWorkInProgressHook();
1434314349

1434414350
if (typeof initialState === "function") {
14345-
// $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
14346-
initialState = initialState();
14351+
var initialStateInitializer = initialState; // $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
14352+
14353+
initialState = initialStateInitializer();
14354+
14355+
if (shouldDoubleInvokeUserFnsInHooksDEV) {
14356+
setIsStrictModeForDevtools(true); // $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
14357+
14358+
initialStateInitializer();
14359+
setIsStrictModeForDevtools(false);
14360+
}
1434714361
}
1434814362

1434914363
hook.memoizedState = hook.baseState = initialState;
@@ -35736,7 +35750,7 @@ if (__DEV__) {
3573635750
return root;
3573735751
}
3573835752

35739-
var ReactVersion = "18.3.0-www-classic-399a1fe7";
35753+
var ReactVersion = "18.3.0-www-classic-81f24f41";
3574035754

3574135755
function createPortal$1(
3574235756
children,

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13696,6 +13696,12 @@ if (__DEV__) {
1369613696

1369713697
if (init !== undefined) {
1369813698
initialState = init(initialArg);
13699+
13700+
if (shouldDoubleInvokeUserFnsInHooksDEV) {
13701+
setIsStrictModeForDevtools(true);
13702+
init(initialArg);
13703+
setIsStrictModeForDevtools(false);
13704+
}
1369913705
} else {
1370013706
initialState = initialArg;
1370113707
}
@@ -14278,8 +14284,16 @@ if (__DEV__) {
1427814284
var hook = mountWorkInProgressHook();
1427914285

1428014286
if (typeof initialState === "function") {
14281-
// $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
14282-
initialState = initialState();
14287+
var initialStateInitializer = initialState; // $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
14288+
14289+
initialState = initialStateInitializer();
14290+
14291+
if (shouldDoubleInvokeUserFnsInHooksDEV) {
14292+
setIsStrictModeForDevtools(true); // $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
14293+
14294+
initialStateInitializer();
14295+
setIsStrictModeForDevtools(false);
14296+
}
1428314297
}
1428414298

1428514299
hook.memoizedState = hook.baseState = initialState;
@@ -35557,7 +35571,7 @@ if (__DEV__) {
3555735571
return root;
3555835572
}
3555935573

35560-
var ReactVersion = "18.3.0-www-modern-db8ad56f";
35574+
var ReactVersion = "18.3.0-www-modern-13ebed85";
3556135575

3556235576
function createPortal$1(
3556335577
children,

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,7 +3823,14 @@ function forceStoreRerender(fiber) {
38233823
}
38243824
function mountStateImpl(initialState) {
38253825
var hook = mountWorkInProgressHook();
3826-
"function" === typeof initialState && (initialState = initialState());
3826+
if ("function" === typeof initialState) {
3827+
var initialStateInitializer = initialState;
3828+
initialState = initialStateInitializer();
3829+
shouldDoubleInvokeUserFnsInHooksDEV &&
3830+
(setIsStrictModeForDevtools(!0),
3831+
initialStateInitializer(),
3832+
setIsStrictModeForDevtools(!1));
3833+
}
38273834
hook.memoizedState = hook.baseState = initialState;
38283835
hook.queue = {
38293836
pending: null,
@@ -4490,14 +4497,20 @@ var HooksDispatcherOnMount = {
44904497
},
44914498
useReducer: function (reducer, initialArg, init) {
44924499
var hook = mountWorkInProgressHook();
4493-
initialArg = void 0 !== init ? init(initialArg) : initialArg;
4494-
hook.memoizedState = hook.baseState = initialArg;
4500+
if (void 0 !== init) {
4501+
var initialState = init(initialArg);
4502+
shouldDoubleInvokeUserFnsInHooksDEV &&
4503+
(setIsStrictModeForDevtools(!0),
4504+
init(initialArg),
4505+
setIsStrictModeForDevtools(!1));
4506+
} else initialState = initialArg;
4507+
hook.memoizedState = hook.baseState = initialState;
44954508
reducer = {
44964509
pending: null,
44974510
lanes: 0,
44984511
dispatch: null,
44994512
lastRenderedReducer: reducer,
4500-
lastRenderedState: initialArg
4513+
lastRenderedState: initialState
45014514
};
45024515
hook.queue = reducer;
45034516
reducer = reducer.dispatch = dispatchReducerAction.bind(
@@ -17148,7 +17161,7 @@ Internals.Events = [
1714817161
var devToolsConfig$jscomp$inline_1823 = {
1714917162
findFiberByHostInstance: getClosestInstanceFromNode,
1715017163
bundleType: 0,
17151-
version: "18.3.0-www-classic-4cdedb24",
17164+
version: "18.3.0-www-classic-e4ba2ec7",
1715217165
rendererPackageName: "react-dom"
1715317166
};
1715417167
var internals$jscomp$inline_2185 = {
@@ -17178,7 +17191,7 @@ var internals$jscomp$inline_2185 = {
1717817191
scheduleRoot: null,
1717917192
setRefreshHandler: null,
1718017193
getCurrentFiber: null,
17181-
reconcilerVersion: "18.3.0-www-classic-4cdedb24"
17194+
reconcilerVersion: "18.3.0-www-classic-e4ba2ec7"
1718217195
};
1718317196
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
1718417197
var hook$jscomp$inline_2186 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
@@ -17528,4 +17541,4 @@ exports.useFormStatus = function () {
1752817541
return ReactCurrentDispatcher$2.current.useHostTransitionStatus();
1752917542
throw Error(formatProdErrorMessage(248));
1753017543
};
17531-
exports.version = "18.3.0-www-classic-4cdedb24";
17544+
exports.version = "18.3.0-www-classic-e4ba2ec7";

0 commit comments

Comments
 (0)