-
Notifications
You must be signed in to change notification settings - Fork 46.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Always keep disabled logs in the second pass #21739
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,7 +169,7 @@ function applyDerivedStateFromProps( | |
nextProps: any, | ||
) { | ||
const prevState = workInProgress.memoizedState; | ||
|
||
let partialState = getDerivedStateFromProps(nextProps, prevState); | ||
if (__DEV__) { | ||
if ( | ||
debugRenderPhaseSideEffectsForStrictMode && | ||
|
@@ -178,16 +178,11 @@ function applyDerivedStateFromProps( | |
disableLogs(); | ||
try { | ||
// Invoke the function an extra time to help detect side-effects. | ||
getDerivedStateFromProps(nextProps, prevState); | ||
partialState = getDerivedStateFromProps(nextProps, prevState); | ||
} finally { | ||
reenableLogs(); | ||
} | ||
} | ||
} | ||
|
||
const partialState = getDerivedStateFromProps(nextProps, prevState); | ||
|
||
if (__DEV__) { | ||
warnOnUndefinedDerivedState(ctor, partialState); | ||
} | ||
// Merge the partial state and the previous state. | ||
|
@@ -323,6 +318,11 @@ function checkShouldComponentUpdate( | |
) { | ||
const instance = workInProgress.stateNode; | ||
if (typeof instance.shouldComponentUpdate === 'function') { | ||
let shouldUpdate = instance.shouldComponentUpdate( | ||
newProps, | ||
newState, | ||
nextContext, | ||
); | ||
if (__DEV__) { | ||
if ( | ||
debugRenderPhaseSideEffectsForStrictMode && | ||
|
@@ -331,19 +331,15 @@ function checkShouldComponentUpdate( | |
disableLogs(); | ||
try { | ||
// Invoke the function an extra time to help detect side-effects. | ||
instance.shouldComponentUpdate(newProps, newState, nextContext); | ||
shouldUpdate = instance.shouldComponentUpdate( | ||
newProps, | ||
newState, | ||
nextContext, | ||
); | ||
} finally { | ||
reenableLogs(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
} | ||
} | ||
} | ||
const shouldUpdate = instance.shouldComponentUpdate( | ||
newProps, | ||
newState, | ||
nextContext, | ||
); | ||
|
||
if (__DEV__) { | ||
if (shouldUpdate === undefined) { | ||
console.error( | ||
'%s.shouldComponentUpdate(): Returned undefined instead of a ' + | ||
|
@@ -659,6 +655,7 @@ function constructClassInstance( | |
: emptyContextObject; | ||
} | ||
|
||
let instance = new ctor(props, context); | ||
// Instantiate twice to help detect side-effects. | ||
if (__DEV__) { | ||
if ( | ||
|
@@ -667,14 +664,13 @@ function constructClassInstance( | |
) { | ||
disableLogs(); | ||
try { | ||
new ctor(props, context); // eslint-disable-line no-new | ||
instance = new ctor(props, context); // eslint-disable-line no-new | ||
} finally { | ||
reenableLogs(); | ||
} | ||
} | ||
} | ||
|
||
const instance = new ctor(props, context); | ||
const state = (workInProgress.memoizedState = | ||
instance.state !== null && instance.state !== undefined | ||
? instance.state | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -892,4 +892,196 @@ describe('context legacy', () => { | |
// Dedupe | ||
ReactDOM.render(<Root />, container); | ||
}); | ||
|
||
describe('disableLogs', () => { | ||
it('disables logs once for class double render', () => { | ||
spyOnDevAndProd(console, 'log'); | ||
|
||
let count = 0; | ||
class Foo extends React.Component { | ||
render() { | ||
count++; | ||
console.log('foo ' + count); | ||
return null; | ||
} | ||
} | ||
|
||
const container = document.createElement('div'); | ||
ReactDOM.render( | ||
<React.StrictMode> | ||
<Foo /> | ||
</React.StrictMode>, | ||
container, | ||
); | ||
|
||
expect(count).toBe(__DEV__ ? 2 : 1); | ||
expect(console.log).toBeCalledTimes(1); | ||
// Note: we should display the first log because otherwise | ||
// there is a risk of suppressing warnings when they happen, | ||
// and on the next render they'd get deduplicated and ignored. | ||
expect(console.log).toBeCalledWith('foo 1'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice tests 👍🏼 |
||
|
||
it('disables logs once for class double ctor', () => { | ||
spyOnDevAndProd(console, 'log'); | ||
|
||
let count = 0; | ||
class Foo extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
count++; | ||
console.log('foo ' + count); | ||
} | ||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
const container = document.createElement('div'); | ||
ReactDOM.render( | ||
<React.StrictMode> | ||
<Foo /> | ||
</React.StrictMode>, | ||
container, | ||
); | ||
|
||
expect(count).toBe(__DEV__ ? 2 : 1); | ||
expect(console.log).toBeCalledTimes(1); | ||
// Note: we should display the first log because otherwise | ||
// there is a risk of suppressing warnings when they happen, | ||
// and on the next render they'd get deduplicated and ignored. | ||
expect(console.log).toBeCalledWith('foo 1'); | ||
}); | ||
|
||
it('disables logs once for class double getDerivedStateFromProps', () => { | ||
spyOnDevAndProd(console, 'log'); | ||
|
||
let count = 0; | ||
class Foo extends React.Component { | ||
state = {}; | ||
static getDerivedStateFromProps() { | ||
count++; | ||
console.log('foo ' + count); | ||
return {}; | ||
} | ||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
const container = document.createElement('div'); | ||
ReactDOM.render( | ||
<React.StrictMode> | ||
<Foo /> | ||
</React.StrictMode>, | ||
container, | ||
); | ||
|
||
expect(count).toBe(__DEV__ ? 2 : 1); | ||
expect(console.log).toBeCalledTimes(1); | ||
// Note: we should display the first log because otherwise | ||
// there is a risk of suppressing warnings when they happen, | ||
// and on the next render they'd get deduplicated and ignored. | ||
expect(console.log).toBeCalledWith('foo 1'); | ||
}); | ||
|
||
it('disables logs once for class double shouldComponentUpdate', () => { | ||
spyOnDevAndProd(console, 'log'); | ||
|
||
let count = 0; | ||
class Foo extends React.Component { | ||
state = {}; | ||
shouldComponentUpdate() { | ||
count++; | ||
console.log('foo ' + count); | ||
return {}; | ||
} | ||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
const container = document.createElement('div'); | ||
ReactDOM.render( | ||
<React.StrictMode> | ||
<Foo /> | ||
</React.StrictMode>, | ||
container, | ||
); | ||
// Trigger sCU: | ||
ReactDOM.render( | ||
<React.StrictMode> | ||
<Foo /> | ||
</React.StrictMode>, | ||
container, | ||
); | ||
|
||
expect(count).toBe(__DEV__ ? 2 : 1); | ||
expect(console.log).toBeCalledTimes(1); | ||
// Note: we should display the first log because otherwise | ||
// there is a risk of suppressing warnings when they happen, | ||
// and on the next render they'd get deduplicated and ignored. | ||
expect(console.log).toBeCalledWith('foo 1'); | ||
}); | ||
|
||
it('disables logs once for class state updaters', () => { | ||
spyOnDevAndProd(console, 'log'); | ||
|
||
let inst; | ||
let count = 0; | ||
class Foo extends React.Component { | ||
state = {}; | ||
render() { | ||
inst = this; | ||
return null; | ||
} | ||
} | ||
|
||
const container = document.createElement('div'); | ||
ReactDOM.render( | ||
<React.StrictMode> | ||
<Foo /> | ||
</React.StrictMode>, | ||
container, | ||
); | ||
inst.setState(() => { | ||
count++; | ||
console.log('foo ' + count); | ||
return {}; | ||
}); | ||
|
||
expect(count).toBe(__DEV__ ? 2 : 1); | ||
expect(console.log).toBeCalledTimes(1); | ||
// Note: we should display the first log because otherwise | ||
// there is a risk of suppressing warnings when they happen, | ||
// and on the next render they'd get deduplicated and ignored. | ||
expect(console.log).toBeCalledWith('foo 1'); | ||
}); | ||
|
||
it('disables logs once for function double render', () => { | ||
spyOnDevAndProd(console, 'log'); | ||
|
||
let count = 0; | ||
function Foo() { | ||
count++; | ||
console.log('foo ' + count); | ||
return null; | ||
} | ||
|
||
const container = document.createElement('div'); | ||
ReactDOM.render( | ||
<React.StrictMode> | ||
<Foo /> | ||
</React.StrictMode>, | ||
container, | ||
); | ||
|
||
expect(count).toBe(__DEV__ ? 2 : 1); | ||
expect(console.log).toBeCalledTimes(1); | ||
// Note: we should display the first log because otherwise | ||
// there is a risk of suppressing warnings when they happen, | ||
// and on the next render they'd get deduplicated and ignored. | ||
expect(console.log).toBeCalledWith('foo 1'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so this PR does more than just disable logs during the second pass. It also changes the code to use the result of the first pass rather than the second pass? Should be okay so long as there are no unsafe side effects?
I think that using the 2nd call (for this and other methods below) may have been an intentional choice in case there were side effects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I haven't thought about this. I do vaguely remember some two year old conversations about this. Maybe let's hold off merging and keep it for the next sync discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool beans 👍🏼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the double rendering order in general is very intentional eg renderWithHooks.
However I noticed when doing the Fizz parts that some of the class ones are reverse order. I'm not sure that was intentional or an oversight when we intentionally did the other ones.
Because I think there are some subtle semantic issues with things like memoization in general that's an issue here. Deduping warnings being one such memoization that breaks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way we do it with Hooks is we disable logs during the second one and use the result of the second one. I could make everything match that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been extremely confused before why the logs and the returned values didn't match... Maybe disabling the logs on the first call and enable logs and use the result of the second call? On the other hand that might make it even harder to deduce whats happening since you can't "see" the first call at all...