Infinite loop when passing {} as data for FlatList
See original GitHub issueDescription
If you pass a non-array object to FlatList
data
property, ReactNative javascript thread gets stuck in a infinite loop, due to FlatList._getItemCount
returning NaN
.
Reproduction Steps
Pass any object that doesn’t has length
property to FlatList
.
Sample Code
https://snack.expo.io/BJrFCR9QZ
Simply: <FlatList data={{}} />
Solution
Change FlatList._getItemCount
to test for the data.length
:
_getItemCount = (data: ?Array<ItemT>): number => {
return (data && data.length) ? Math.ceil(data.length / this.props.numColumns) : 0;
};
https://github.com/facebook/react-native/blob/master/Libraries/Lists/FlatList.js#L470
Additional Information
- React Native version: “0.44.0”,
- Platform: any
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Re render flat list when data change cause infinite loop React ...
If I pass value on second argument on UseEffect, it cause infinite loop on API request. If not, my FlatList cant re render...
Read more >react native flatlist infinite loop - You.com | The AI Search ...
If you pass a non-array object to FlatList data property, ReactNative javascript thread gets stuck in a infinite loop, due to FlatList.
Read more >[Solved]-Flatlist onEndReached endless loop-Reactjs
You are using a component that is being rendered when you are loading data, right? So your flatlist needs to rerender. You meed...
Read more >Displaying a List in React Native: Map Method or FlatList ...
In this article, I'll walk you through using two methods of listing data in a React Native app: displaying the list with map...
Read more >ListView - React Native
DEPRECATED - use one of the new list components, such as FlatList or ... potentially very large (or conceptually infinite) data sets:.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Benzer1406 No, I haven’t bothered to create a new issue, yet. I started looking at the source myself and try to see if I could fix it myself.
Unfortunately the tests run by
npm test
doesn’t seem to be testing the actual execution of the code, and the flow testsnpm run flow
seems to be testing type information code that seems to be ignored when the actual code is executed.The tests for the List library seems to mock (too) much of the actual execution; and it seems to be content if the
FlatList
tag is somehow translated into aRCTScrollView
and this conversion byReactTestRenderer.create
does not seem to invoke the infinite loop bug.The flow tests seem to cause errors about bad datatype when the data property is not an array; while in production code you can put all sort of junk in that property and it will either work (if it’s a valid array) or either an empty list or infinite loop if it’s not; no warnings is ever produced as long as it’s not
null
.I have to somehow figure out a way to write a test that actually causes the infinite loop (that I know is still there) so that I can verify that any fix actually fixes the problem.
@strindhaug Thanks for your detailed answer. Yes, I was figuring out and pondering about the fact that the problem was indeed an issue of a bad datatype. I could just figure out up to that point and reiterate the loop, but have no idea what it causes or what needs to be done to get out of the loop. Meanwhile I changed to the classic
<ListView>
even though it is deprecated, but at least it works.