Skip to content

Commit adfa3d4

Browse files
committed
Always use the second result
1 parent 96d2a7b commit adfa3d4

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

packages/react-reconciler/src/ReactFiberClassComponent.new.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,21 @@ function applyDerivedStateFromProps(
169169
nextProps: any,
170170
) {
171171
const prevState = workInProgress.memoizedState;
172-
const partialState = getDerivedStateFromProps(nextProps, prevState);
172+
let partialState = getDerivedStateFromProps(nextProps, prevState);
173173
if (__DEV__) {
174-
warnOnUndefinedDerivedState(ctor, partialState);
175174
if (
176175
debugRenderPhaseSideEffectsForStrictMode &&
177176
workInProgress.mode & StrictLegacyMode
178177
) {
179178
disableLogs();
180179
try {
181180
// Invoke the function an extra time to help detect side-effects.
182-
getDerivedStateFromProps(nextProps, prevState);
181+
partialState = getDerivedStateFromProps(nextProps, prevState);
183182
} finally {
184183
reenableLogs();
185184
}
186185
}
186+
warnOnUndefinedDerivedState(ctor, partialState);
187187
}
188188
// Merge the partial state and the previous state.
189189
const memoizedState =
@@ -318,31 +318,35 @@ function checkShouldComponentUpdate(
318318
) {
319319
const instance = workInProgress.stateNode;
320320
if (typeof instance.shouldComponentUpdate === 'function') {
321-
const shouldUpdate = instance.shouldComponentUpdate(
321+
let shouldUpdate = instance.shouldComponentUpdate(
322322
newProps,
323323
newState,
324324
nextContext,
325325
);
326326
if (__DEV__) {
327-
if (shouldUpdate === undefined) {
328-
console.error(
329-
'%s.shouldComponentUpdate(): Returned undefined instead of a ' +
330-
'boolean value. Make sure to return true or false.',
331-
getComponentNameFromType(ctor) || 'Component',
332-
);
333-
}
334327
if (
335328
debugRenderPhaseSideEffectsForStrictMode &&
336329
workInProgress.mode & StrictLegacyMode
337330
) {
338331
disableLogs();
339332
try {
340333
// Invoke the function an extra time to help detect side-effects.
341-
instance.shouldComponentUpdate(newProps, newState, nextContext);
334+
shouldUpdate = instance.shouldComponentUpdate(
335+
newProps,
336+
newState,
337+
nextContext,
338+
);
342339
} finally {
343340
reenableLogs();
344341
}
345342
}
343+
if (shouldUpdate === undefined) {
344+
console.error(
345+
'%s.shouldComponentUpdate(): Returned undefined instead of a ' +
346+
'boolean value. Make sure to return true or false.',
347+
getComponentNameFromType(ctor) || 'Component',
348+
);
349+
}
346350
}
347351

348352
return shouldUpdate;
@@ -651,7 +655,7 @@ function constructClassInstance(
651655
: emptyContextObject;
652656
}
653657

654-
const instance = new ctor(props, context);
658+
let instance = new ctor(props, context);
655659
// Instantiate twice to help detect side-effects.
656660
if (__DEV__) {
657661
if (
@@ -660,7 +664,7 @@ function constructClassInstance(
660664
) {
661665
disableLogs();
662666
try {
663-
new ctor(props, context); // eslint-disable-line no-new
667+
instance = new ctor(props, context); // eslint-disable-line no-new
664668
} finally {
665669
reenableLogs();
666670
}

packages/react-reconciler/src/ReactFiberClassComponent.old.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,21 @@ function applyDerivedStateFromProps(
169169
nextProps: any,
170170
) {
171171
const prevState = workInProgress.memoizedState;
172-
const partialState = getDerivedStateFromProps(nextProps, prevState);
172+
let partialState = getDerivedStateFromProps(nextProps, prevState);
173173
if (__DEV__) {
174-
warnOnUndefinedDerivedState(ctor, partialState);
175174
if (
176175
debugRenderPhaseSideEffectsForStrictMode &&
177176
workInProgress.mode & StrictLegacyMode
178177
) {
179178
disableLogs();
180179
try {
181180
// Invoke the function an extra time to help detect side-effects.
182-
getDerivedStateFromProps(nextProps, prevState);
181+
partialState = getDerivedStateFromProps(nextProps, prevState);
183182
} finally {
184183
reenableLogs();
185184
}
186185
}
186+
warnOnUndefinedDerivedState(ctor, partialState);
187187
}
188188
// Merge the partial state and the previous state.
189189
const memoizedState =
@@ -318,31 +318,35 @@ function checkShouldComponentUpdate(
318318
) {
319319
const instance = workInProgress.stateNode;
320320
if (typeof instance.shouldComponentUpdate === 'function') {
321-
const shouldUpdate = instance.shouldComponentUpdate(
321+
let shouldUpdate = instance.shouldComponentUpdate(
322322
newProps,
323323
newState,
324324
nextContext,
325325
);
326326
if (__DEV__) {
327-
if (shouldUpdate === undefined) {
328-
console.error(
329-
'%s.shouldComponentUpdate(): Returned undefined instead of a ' +
330-
'boolean value. Make sure to return true or false.',
331-
getComponentNameFromType(ctor) || 'Component',
332-
);
333-
}
334327
if (
335328
debugRenderPhaseSideEffectsForStrictMode &&
336329
workInProgress.mode & StrictLegacyMode
337330
) {
338331
disableLogs();
339332
try {
340333
// Invoke the function an extra time to help detect side-effects.
341-
instance.shouldComponentUpdate(newProps, newState, nextContext);
334+
shouldUpdate = instance.shouldComponentUpdate(
335+
newProps,
336+
newState,
337+
nextContext,
338+
);
342339
} finally {
343340
reenableLogs();
344341
}
345342
}
343+
if (shouldUpdate === undefined) {
344+
console.error(
345+
'%s.shouldComponentUpdate(): Returned undefined instead of a ' +
346+
'boolean value. Make sure to return true or false.',
347+
getComponentNameFromType(ctor) || 'Component',
348+
);
349+
}
346350
}
347351

348352
return shouldUpdate;
@@ -651,7 +655,7 @@ function constructClassInstance(
651655
: emptyContextObject;
652656
}
653657

654-
const instance = new ctor(props, context);
658+
let instance = new ctor(props, context);
655659
// Instantiate twice to help detect side-effects.
656660
if (__DEV__) {
657661
if (
@@ -660,7 +664,7 @@ function constructClassInstance(
660664
) {
661665
disableLogs();
662666
try {
663-
new ctor(props, context); // eslint-disable-line no-new
667+
instance = new ctor(props, context); // eslint-disable-line no-new
664668
} finally {
665669
reenableLogs();
666670
}

0 commit comments

Comments
 (0)