Skip to content

Make warning about state assignment better in ReactFiberClassComponent #11830

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const isArray = Array.isArray;

let didWarnAboutStateAssignmentForComponent;
let warnOnInvalidCallback;
let warnAboutStateAssignment;

if (__DEV__) {
didWarnAboutStateAssignmentForComponent = {};
Expand All @@ -56,6 +57,20 @@ if (__DEV__) {
);
};

warnAboutStateAssignment = function(
componentName: string,
lifeCycle: string,
) {
warning(
false,
'%s.%s(): Assigning directly to this.state is ' +
"deprecated (except inside a component's " +
'constructor). Use setState instead.',
componentName,
lifeCycle,
);
};

// This is so gross but it's at least non-critical and can be removed if
// it causes problems. This is meant to give a nicer error message for
// ReactDOM15.unstable_renderSubtreeIntoContainer(reactDOM16Component,
Expand Down Expand Up @@ -389,13 +404,8 @@ export default function(

if (oldState !== instance.state) {
if (__DEV__) {
warning(
false,
'%s.componentWillMount(): Assigning directly to this.state is ' +
"deprecated (except inside a component's " +
'constructor). Use setState instead.',
getComponentName(workInProgress),
);
const componentName = getComponentName(workInProgress) || 'Component';
warnAboutStateAssignment(componentName, 'componentWillMount');
}
updater.enqueueReplaceState(instance, instance.state, null);
}
Expand All @@ -421,13 +431,7 @@ export default function(
if (__DEV__) {
const componentName = getComponentName(workInProgress) || 'Component';
if (!didWarnAboutStateAssignmentForComponent[componentName]) {
warning(
false,
'%s.componentWillReceiveProps(): Assigning directly to ' +
"this.state is deprecated (except inside a component's " +
'constructor). Use setState instead.',
componentName,
);
warnAboutStateAssignment(componentName, 'componentWillReceiveProps');
didWarnAboutStateAssignmentForComponent[componentName] = true;
}
}
Expand Down