Skip to content

Commit 6ae2c33

Browse files
author
Brian Vaughn
authored
StrictMode should call sCU twice in DEV (#17942)
1 parent 9dbe1c5 commit 6ae2c33

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

packages/react-reconciler/src/ReactFiberClassComponent.js

+9
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ function checkShouldComponentUpdate(
262262
) {
263263
const instance = workInProgress.stateNode;
264264
if (typeof instance.shouldComponentUpdate === 'function') {
265+
if (__DEV__) {
266+
if (
267+
debugRenderPhaseSideEffectsForStrictMode &&
268+
workInProgress.mode & StrictMode
269+
) {
270+
// Invoke the function an extra time to help detect side-effects.
271+
instance.shouldComponentUpdate(newProps, newState, nextContext);
272+
}
273+
}
265274
startPhaseTimer(workInProgress, 'shouldComponentUpdate');
266275
const shouldUpdate = instance.shouldComponentUpdate(
267276
newProps,

packages/react/src/__tests__/ReactStrictMode-test.js

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ describe('ReactStrictMode', () => {
138138
'getDerivedStateFromProps',
139139
'getDerivedStateFromProps',
140140
'shouldComponentUpdate',
141+
'shouldComponentUpdate',
141142
'render',
142143
'render',
143144
'componentDidUpdate',
@@ -166,6 +167,7 @@ describe('ReactStrictMode', () => {
166167
'getDerivedStateFromProps',
167168
'getDerivedStateFromProps',
168169
'shouldComponentUpdate',
170+
'shouldComponentUpdate',
169171
]);
170172
} else {
171173
expect(log).toEqual([
@@ -283,6 +285,7 @@ describe('ReactStrictMode', () => {
283285
'getDerivedStateFromProps',
284286
'getDerivedStateFromProps',
285287
'shouldComponentUpdate',
288+
'shouldComponentUpdate',
286289
'render',
287290
'render',
288291
'componentDidUpdate',
@@ -305,6 +308,7 @@ describe('ReactStrictMode', () => {
305308
'getDerivedStateFromProps',
306309
'getDerivedStateFromProps',
307310
'shouldComponentUpdate',
311+
'shouldComponentUpdate',
308312
]);
309313
} else {
310314
expect(log).toEqual([

0 commit comments

Comments
 (0)