-
Notifications
You must be signed in to change notification settings - Fork 48.8k
React.pure automatically forwards ref #13822
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -35,14 +35,14 @@ describe('pure', () => { | |
} | ||
|
||
// Tests should run against both the lazy and non-lazy versions of `pure`. | ||
// To make the tests work for both versions, we wrap the non-lazy verion in | ||
// To make the tests work for both versions, we wrap the non-lazy version in | ||
// a lazy function component. | ||
sharedTests('normal', (...args) => { | ||
const Pure = React.pure(...args); | ||
function Indirection(props) { | ||
return <Pure {...props} />; | ||
function Indirection(props, ref) { | ||
return <Pure {...props} ref={ref} />; | ||
} | ||
return Promise.resolve(Indirection); | ||
return Promise.resolve(React.forwardRef(Indirection)); | ||
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. Ironically had to figure out how to compose these for the sole purpose of testing this change. |
||
}); | ||
sharedTests('lazy', (...args) => Promise.resolve(React.pure(...args))); | ||
|
||
|
@@ -84,110 +84,169 @@ describe('pure', () => { | |
expect(ReactNoop.flush()).toEqual([1]); | ||
expect(ReactNoop.getChildren()).toEqual([span(1)]); | ||
}); | ||
}); | ||
|
||
it("does not bail out if there's a context change", async () => { | ||
const {unstable_Suspense: Suspense} = React; | ||
|
||
const CountContext = React.createContext(0); | ||
|
||
function Counter(props) { | ||
const count = CountContext.unstable_read(); | ||
return <Text text={`${props.label}: ${count}`} />; | ||
} | ||
Counter = pure(Counter); | ||
|
||
class Parent extends React.Component { | ||
state = {count: 0}; | ||
render() { | ||
return ( | ||
<Suspense> | ||
<CountContext.Provider value={this.state.count}> | ||
<Counter label="Count" /> | ||
</CountContext.Provider> | ||
</Suspense> | ||
it("does not bail out if there's a context change", async () => { | ||
const {unstable_Suspense: Suspense} = React; | ||
|
||
const CountContext = React.createContext(0); | ||
|
||
function Counter(props) { | ||
const count = CountContext.unstable_read(); | ||
return <Text text={`${props.label}: ${count}`} />; | ||
} | ||
Counter = pure(Counter); | ||
|
||
class Parent extends React.Component { | ||
state = {count: 0}; | ||
render() { | ||
return ( | ||
<Suspense> | ||
<CountContext.Provider value={this.state.count}> | ||
<Counter label="Count" /> | ||
</CountContext.Provider> | ||
</Suspense> | ||
); | ||
} | ||
} | ||
|
||
const parent = React.createRef(null); | ||
ReactNoop.render(<Parent ref={parent} />); | ||
expect(ReactNoop.flush()).toEqual([]); | ||
await Promise.resolve(); | ||
expect(ReactNoop.flush()).toEqual(['Count: 0']); | ||
expect(ReactNoop.getChildren()).toEqual([span('Count: 0')]); | ||
|
||
// Should bail out because props have not changed | ||
ReactNoop.render(<Parent ref={parent} />); | ||
expect(ReactNoop.flush()).toEqual([]); | ||
expect(ReactNoop.getChildren()).toEqual([span('Count: 0')]); | ||
|
||
// Should update because there was a context change | ||
parent.current.setState({count: 1}); | ||
expect(ReactNoop.flush()).toEqual(['Count: 1']); | ||
expect(ReactNoop.getChildren()).toEqual([span('Count: 1')]); | ||
}); | ||
|
||
it('accepts custom comparison function', async () => { | ||
const {unstable_Suspense: Suspense} = React; | ||
|
||
function Counter({count}) { | ||
return <Text text={count} />; | ||
} | ||
Counter = pure(Counter, (oldProps, newProps) => { | ||
ReactNoop.yield( | ||
`Old count: ${oldProps.count}, New count: ${newProps.count}`, | ||
); | ||
return oldProps.count === newProps.count; | ||
}); | ||
|
||
ReactNoop.render( | ||
<Suspense> | ||
<Counter count={0} /> | ||
</Suspense>, | ||
); | ||
expect(ReactNoop.flush()).toEqual([]); | ||
await Promise.resolve(); | ||
expect(ReactNoop.flush()).toEqual([0]); | ||
expect(ReactNoop.getChildren()).toEqual([span(0)]); | ||
|
||
// Should bail out because props have not changed | ||
ReactNoop.render( | ||
<Suspense> | ||
<Counter count={0} /> | ||
</Suspense>, | ||
); | ||
expect(ReactNoop.flush()).toEqual(['Old count: 0, New count: 0']); | ||
expect(ReactNoop.getChildren()).toEqual([span(0)]); | ||
|
||
// Should update because count prop changed | ||
ReactNoop.render( | ||
<Suspense> | ||
<Counter count={1} /> | ||
</Suspense>, | ||
); | ||
expect(ReactNoop.flush()).toEqual(['Old count: 0, New count: 1', 1]); | ||
expect(ReactNoop.getChildren()).toEqual([span(1)]); | ||
}); | ||
|
||
it('warns for class components', () => { | ||
class SomeClass extends React.Component { | ||
render() { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
const parent = React.createRef(null); | ||
ReactNoop.render(<Parent ref={parent} />); | ||
expect(ReactNoop.flush()).toEqual([]); | ||
await Promise.resolve(); | ||
expect(ReactNoop.flush()).toEqual(['Count: 0']); | ||
expect(ReactNoop.getChildren()).toEqual([span('Count: 0')]); | ||
|
||
// Should bail out because props have not changed | ||
ReactNoop.render(<Parent ref={parent} />); | ||
expect(ReactNoop.flush()).toEqual([]); | ||
expect(ReactNoop.getChildren()).toEqual([span('Count: 0')]); | ||
|
||
// Should update because there was a context change | ||
parent.current.setState({count: 1}); | ||
expect(ReactNoop.flush()).toEqual(['Count: 1']); | ||
expect(ReactNoop.getChildren()).toEqual([span('Count: 1')]); | ||
}); | ||
expect(() => pure(SomeClass)).toWarnDev( | ||
'pure: The first argument must be a function component.', | ||
{withoutStack: true}, | ||
); | ||
}); | ||
|
||
it('accepts custom comparison function', async () => { | ||
const {unstable_Suspense: Suspense} = React; | ||
it('warns if first argument is not a function', () => { | ||
expect(() => pure()).toWarnDev( | ||
'pure: The first argument must be a function component. Instead ' + | ||
'received: undefined', | ||
{withoutStack: true}, | ||
); | ||
}); | ||
|
||
function Counter({count}) { | ||
return <Text text={count} />; | ||
} | ||
Counter = pure(Counter, (oldProps, newProps) => { | ||
ReactNoop.yield( | ||
`Old count: ${oldProps.count}, New count: ${newProps.count}`, | ||
it('forwards ref', async () => { | ||
const {unstable_Suspense: Suspense} = React; | ||
const Transparent = pure((props, ref) => { | ||
return <div ref={ref} />; | ||
}); | ||
const divRef = React.createRef(); | ||
|
||
ReactNoop.render( | ||
<Suspense> | ||
<Transparent ref={divRef} /> | ||
</Suspense>, | ||
); | ||
return oldProps.count === newProps.count; | ||
ReactNoop.flush(); | ||
await Promise.resolve(); | ||
ReactNoop.flush(); | ||
expect(divRef.current.type).toBe('div'); | ||
}); | ||
|
||
ReactNoop.render( | ||
<Suspense> | ||
<Counter count={0} /> | ||
</Suspense>, | ||
); | ||
expect(ReactNoop.flush()).toEqual([]); | ||
await Promise.resolve(); | ||
expect(ReactNoop.flush()).toEqual([0]); | ||
expect(ReactNoop.getChildren()).toEqual([span(0)]); | ||
|
||
// Should bail out because props have not changed | ||
ReactNoop.render( | ||
<Suspense> | ||
<Counter count={0} /> | ||
</Suspense>, | ||
); | ||
expect(ReactNoop.flush()).toEqual(['Old count: 0, New count: 0']); | ||
expect(ReactNoop.getChildren()).toEqual([span(0)]); | ||
|
||
// Should update because count prop changed | ||
ReactNoop.render( | ||
<Suspense> | ||
<Counter count={1} /> | ||
</Suspense>, | ||
); | ||
expect(ReactNoop.flush()).toEqual(['Old count: 0, New count: 1', 1]); | ||
expect(ReactNoop.getChildren()).toEqual([span(1)]); | ||
}); | ||
it('updates if only ref changes', async () => { | ||
const {unstable_Suspense: Suspense} = React; | ||
const Transparent = pure((props, ref) => { | ||
return [<Text key="text" text="Text" />, <div key="div" ref={ref} />]; | ||
}); | ||
|
||
it('warns for class components', () => { | ||
class SomeClass extends React.Component { | ||
render() { | ||
return null; | ||
} | ||
} | ||
expect(() => pure(SomeClass)).toWarnDev( | ||
'pure: The first argument must be a function component.', | ||
{withoutStack: true}, | ||
); | ||
}); | ||
const divRef = React.createRef(); | ||
const divRef2 = React.createRef(); | ||
|
||
ReactNoop.render( | ||
<Suspense> | ||
<Transparent ref={divRef} /> | ||
</Suspense>, | ||
); | ||
expect(ReactNoop.flush()).toEqual([]); | ||
await Promise.resolve(); | ||
expect(ReactNoop.flush()).toEqual(['Text']); | ||
expect(divRef.current.type).toBe('div'); | ||
expect(divRef2.current).toBe(null); | ||
|
||
// Should re-render (new ref) | ||
ReactNoop.render( | ||
<Suspense> | ||
<Transparent ref={divRef2} /> | ||
</Suspense>, | ||
); | ||
expect(ReactNoop.flush()).toEqual(['Text']); | ||
expect(divRef.current).toBe(null); | ||
expect(divRef2.current.type).toBe('div'); | ||
|
||
it('warns if first argument is not a function', () => { | ||
expect(() => pure()).toWarnDev( | ||
'pure: The first argument must be a function component. Instead ' + | ||
'received: undefined', | ||
{withoutStack: true}, | ||
); | ||
// Should not re-render (same ref) | ||
ReactNoop.render( | ||
<Suspense> | ||
<Transparent ref={divRef2} /> | ||
</Suspense>, | ||
); | ||
expect(ReactNoop.flush()).toEqual([]); | ||
expect(divRef.current).toBe(null); | ||
expect(divRef2.current.type).toBe('div'); | ||
}); | ||
}); | ||
} | ||
}); |
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.
Thanks! Need to fix this for
forwardRef
, too, but we can do that in a separate PR.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.
Looks like we do already:
react/packages/react-reconciler/src/ReactFiberBeginWork.js
Lines 187 to 208 in 0af8199
though I'm not sure we need to check legacy context there?
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.
Problem is we never hit this code path because of:
react/packages/react-reconciler/src/ReactFiberBeginWork.js
Lines 1209 to 1217 in 0af8199