VirtualizedList.RenderItem throws when using function component with hooks#24832
VirtualizedList.RenderItem throws when using function component with hooks#24832adkenyon wants to merge 13 commits into
Conversation
cpojer
left a comment
There was a problem hiding this comment.
Nice! Thank you for fixing this issue.
facebook-github-bot
left a comment
There was a problem hiding this comment.
@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
I spent some time thinking about this and I'm worried about the performance implications of this breaking change because of the added wrapping for each cell. I think How about this:
Then in the future we can remove |
analysis-bot
left a comment
There was a problem hiding this comment.
Code analysis results:
flowfound some issues.
analysis-bot
left a comment
There was a problem hiding this comment.
Code analysis results:
eslintfound some issues. Runyarn lint --fixto automatically fix problems.
|
@cpojer Implemented your suggestions.
|
facebook-github-bot
left a comment
There was a problem hiding this comment.
@cpojer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
sahrens
left a comment
There was a problem hiding this comment.
Awesome, much cleaner :)
|
I applied the change internally and I'm trying to land it :) Thanks for going through all the iterations with us @adkenyon! |
|
This pull request was successfully merged by @adkenyon in 57a1e7c. When will my fix make it into a release? | Upcoming Releases |
…hooks (facebook#24832) Summary: `<VirtualizedList />` will throw an error if the `renderItem` Prop component uses hooks. Function components without hooks and class components work without issue. Super contrived Example ```{js} function FlatListItem({ item }) { React.useEffect(() => console.log(item),[]) return (<Text>{item}</Text>); } <FlatList data={[1, 2, 3]} renderItem={FlatListItem} /> ``` Example Error: ``` Invariant Violation: Hooks can only be called inside the body of a function component. (https://fb.me/react-invalid-hook-call) This error is located at: in CellRenderer (at VirtualizedList.js:688) in RCTScrollContentView (at ScrollView.js:976) in RCTScrollView (at ScrollView.js:1115) in ScrollView (at VirtualizedList.js:1081) in VirtualizedList (at FlatList.js:632) in FlatList (at WithoutScrollbars.js:21) ... ``` ## Changelog [General] [Added] - VirtualizedList ListItemComponent. An alternative to renderItem that accepts function components with hooks. [General][Added] - FlatList ListItemComponent. An alternative to renderItem that accepts function components with hooks. [General][Added] - VirtualizedList and FlatList tests and updated RNTester example Pull Request resolved: facebook#24832 Reviewed By: sahrens Differential Revision: D15334020 Pulled By: cpojer fbshipit-source-id: 882db722fd6e22f07260b08091b3456d1c66c2c8 (cherry picked from commit 57a1e7c)
…hooks (facebook#24832) Summary: `<VirtualizedList />` will throw an error if the `renderItem` Prop component uses hooks. Function components without hooks and class components work without issue. Super contrived Example ```{js} function FlatListItem({ item }) { React.useEffect(() => console.log(item),[]) return (<Text>{item}</Text>); } <FlatList data={[1, 2, 3]} renderItem={FlatListItem} /> ``` Example Error: ``` Invariant Violation: Hooks can only be called inside the body of a function component. (https://fb.me/react-invalid-hook-call) This error is located at: in CellRenderer (at VirtualizedList.js:688) in RCTScrollContentView (at ScrollView.js:976) in RCTScrollView (at ScrollView.js:1115) in ScrollView (at VirtualizedList.js:1081) in VirtualizedList (at FlatList.js:632) in FlatList (at WithoutScrollbars.js:21) ... ``` ## Changelog [General] [Added] - VirtualizedList ListItemComponent. An alternative to renderItem that accepts function components with hooks. [General][Added] - FlatList ListItemComponent. An alternative to renderItem that accepts function components with hooks. [General][Added] - VirtualizedList and FlatList tests and updated RNTester example Pull Request resolved: facebook#24832 Reviewed By: sahrens Differential Revision: D15334020 Pulled By: cpojer fbshipit-source-id: 882db722fd6e22f07260b08091b3456d1c66c2c8
Summary
<VirtualizedList />will throw an error if therenderItemProp component uses hooks. Function components without hooks and class components work without issue.Super contrived Example
Example Error:
Changelog
[General] [Added] - VirtualizedList ListItemComponent. An alternative to renderItem that accepts function components with hooks.
[General][Added] - FlatList ListItemComponent. An alternative to renderItem that accepts function components with hooks.
[General][Added] - VirtualizedList and FlatList tests and updated RNTester example
Test Plan
Updated JS tests for VirtualizedList and tested on local build.