Skip to content

Commit

Permalink
Don't warn outside of render
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Mar 17, 2020
1 parent 959b279 commit 933157f
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 6 deletions.
110 changes: 110 additions & 0 deletions packages/react-dom/src/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1749,4 +1749,114 @@ describe('ReactCompositeComponent', () => {
ReactDOM.render(<Shadow />, container);
expect(container.firstChild.tagName).toBe('DIV');
});

it('should not warn on updating function component from componentWillMount', () => {
let _setState;
function A() {
_setState = React.useState()[1];
return null;
}
class B extends React.Component {
UNSAFE_componentWillMount() {
_setState({});
}
render() {
return null;
}
}
function Parent() {
return (
<div>
<A />
<B />
</div>
);
}
const container = document.createElement('div');
ReactDOM.render(<Parent />, container);
});

it('should not warn on updating function component from componentWillUpdate', () => {
let _setState;
function A() {
_setState = React.useState()[1];
return null;
}
class B extends React.Component {
UNSAFE_componentWillUpdate() {
_setState({});
}
render() {
return null;
}
}
function Parent() {
return (
<div>
<A />
<B />
</div>
);
}
const container = document.createElement('div');
ReactDOM.render(<Parent />, container);
ReactDOM.render(<Parent />, container);
});

it('should not warn on updating function component from componentWillReceiveProps', () => {
let _setState;
function A() {
_setState = React.useState()[1];
return null;
}
class B extends React.Component {
UNSAFE_componentWillReceiveProps() {
_setState({});
}
render() {
return null;
}
}
function Parent() {
return (
<div>
<A />
<B />
</div>
);
}
const container = document.createElement('div');
ReactDOM.render(<Parent />, container);
ReactDOM.render(<Parent />, container);
});

it('should warn on updating function component from render', () => {
let _setState;
function A() {
_setState = React.useState()[1];
return null;
}
class B extends React.Component {
render() {
_setState({});
return null;
}
}
function Parent() {
return (
<div>
<A />
<B />
</div>
);
}
const container = document.createElement('div');
expect(() => {
ReactDOM.render(<Parent />, container);
}).toErrorDev(
'Cannot update a component (`A`) while rendering a different component (`B`)',
);
// Dedupe.
ReactDOM.render(<Parent />, container);
});
});
2 changes: 2 additions & 0 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ function mountIndeterminateComponent(
ReactStrictModeWarnings.recordLegacyContextWarning(workInProgress, null);
}

setIsRendering(true);
ReactCurrentOwner.current = workInProgress;
value = renderWithHooks(
null,
Expand All @@ -1372,6 +1373,7 @@ function mountIndeterminateComponent(
context,
renderExpirationTime,
);
setIsRendering(false);
} else {
value = renderWithHooks(
null,
Expand Down
12 changes: 6 additions & 6 deletions packages/react-reconciler/src/ReactFiberWorkLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2927,7 +2927,10 @@ if (__DEV__) {

function warnAboutRenderPhaseUpdatesInDEV(fiber) {
if (__DEV__) {
if ((executionContext & RenderContext) !== NoContext) {
if (
ReactCurrentDebugFiberIsRenderingInDEV &&
(executionContext & RenderContext) !== NoContext
) {
switch (fiber.tag) {
case FunctionComponent:
case ForwardRef:
Expand All @@ -2953,18 +2956,15 @@ function warnAboutRenderPhaseUpdatesInDEV(fiber) {
break;
}
case ClassComponent: {
if (
ReactCurrentDebugFiberIsRenderingInDEV &&
!didWarnAboutUpdateInRender
) {
if (!didWarnAboutUpdateInRender) {
console.error(
'Cannot update during an existing state transition (such as ' +
'within `render`). Render methods should be a pure ' +
'function of props and state.',
);
didWarnAboutUpdateInRender = true;
break;
}
break;
}
}
}
Expand Down

0 comments on commit 933157f

Please sign in to comment.