question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infinite loop when passing {} as data for FlatList

See original GitHub issue

Description

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:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
strindhaugcommented, Mar 2, 2018

@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 tests npm 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 a RCTScrollView and this conversion by ReactTestRenderer.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.

0reactions
Benzer1406commented, Mar 2, 2018

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found