Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update React.memo and React.lazy to account for refs
Summary: These higher-order components return a component with the same instance type as the wrapped component. In other words, they have forwardRef-like behavior. This became important after Flow v0.100 which changed the React$ComponentType definition to have a mixed instance type instead of any. Before v0.100, people were able to pass refs to memo/lazy components because it was unchecked. Now we are overly strict, making it impossible to use refs with these components. Reviewed By: jbrown215 Differential Revision: D15639704 fbshipit-source-id: e3e9695759e1a792c5af54dce7511e99077af1cf
- Loading branch information
1 parent
efa3cc5
commit 76b78fa
Showing
5 changed files
with
88 additions
and
18 deletions.
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//@flow | ||
|
||
const React = require('react'); | ||
const {useImperativeHandle} = React; | ||
|
||
function Demo(props, ref) { | ||
useImperativeHandle(ref, () => ({ | ||
moo(x: string) {}, | ||
})); | ||
return null; | ||
} | ||
|
||
const Lazy = React.lazy(async () => ({ | ||
default: React.forwardRef(Demo), | ||
})); | ||
|
||
function App() { | ||
// Error below: moo expects a string, given a number | ||
return ( | ||
<React.Suspense fallback="Loading..."> | ||
<Lazy ref={ref => ref && ref.moo(0)} />; | ||
</React.Suspense> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//@flow | ||
|
||
const React = require('react'); | ||
const {useImperativeHandle} = React; | ||
|
||
function Demo(props, ref) { | ||
useImperativeHandle(ref, () => ({ | ||
moo(x: string) {}, | ||
})); | ||
return null; | ||
} | ||
|
||
const Memo = React.memo(React.forwardRef(Demo)); | ||
|
||
function App() { | ||
// Error below: moo expects a string, given a number | ||
return <Memo ref={ref => ref && ref.moo(0)} />; | ||
} |
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