Skip to content

Commit 500c8aa

Browse files
authored
Add component name to StrictMode error message (#25718)
The error message to warn user about state update coming from inside an update function does not contain name of the offending component. Other warnings StrictMode has, always have offending component mentioned in top level error message. Previous error message: ``` An update (setState, replaceState, or forceUpdate) was scheduled from inside an update function. Update functions should be pure with zero side-effects. Consider using componentDidUpdate or a callback. ``` New error message: ``` An update (setState, replaceState, or forceUpdate) was scheduled from inside an update function. Update functions should be pure with zero side-effects. Consider using componentDidUpdate or a callback. Please update the following component: Foo ```
1 parent 353c302 commit 500c8aa

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

packages/react-reconciler/src/ReactFiberClassUpdateQueue.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import {
108108
ShouldCapture,
109109
DidCapture,
110110
} from './ReactFiberFlags';
111+
import getComponentNameFromFiber from './getComponentNameFromFiber';
111112

112113
import {debugRenderPhaseSideEffectsForStrictMode} from 'shared/ReactFeatureFlags';
113114

@@ -239,11 +240,13 @@ export function enqueueUpdate<State>(
239240
currentlyProcessingQueue === sharedQueue &&
240241
!didWarnUpdateInsideUpdate
241242
) {
243+
const componentName = getComponentNameFromFiber(fiber);
242244
console.error(
243245
'An update (setState, replaceState, or forceUpdate) was scheduled ' +
244246
'from inside an update function. Update functions should be pure, ' +
245247
'with zero side-effects. Consider using componentDidUpdate or a ' +
246-
'callback.',
248+
'callback.\n\nPlease update the following component: %s',
249+
componentName,
247250
);
248251
didWarnUpdateInsideUpdate = true;
249252
}

packages/react-reconciler/src/__tests__/ReactIncrementalUpdates-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ describe('ReactIncrementalUpdates', () => {
474474
'An update (setState, replaceState, or forceUpdate) was scheduled ' +
475475
'from inside an update function. Update functions should be pure, ' +
476476
'with zero side-effects. Consider using componentDidUpdate or a ' +
477-
'callback.',
477+
'callback.\n\nPlease update the following component: Foo',
478478
);
479479
expect(instance.state).toEqual({a: 'a', b: 'b'});
480480

0 commit comments

Comments
 (0)