[Please Help] Click Event on FlatList items not working during setInterval()
See original GitHub issueI have a horizontal FlatList, where each time it reaches the end, it automatically adds new elements to the list, so it kind of is an infinite list. I want the app to scroll through the list by itself automatically, but during my testing, when I use timer to call FlatList’s scrollToOffset() and make the FlatList auto scrolling, I can not click the item (onPress={this.clickItem} never get called), I guess it has something to do with the time set for setInterval(func, time) because when I increase it to a high value it works (but of course the scrolling is not nice).
May I ask whats the correct way to implement such function?
Any help would be much appreciated!
Timer function:
this.timer = setInterval(() => {
console.log('I do not leak!');
this.flatListRef.scrollToOffset({ offset: this.scrollX+5});
}, 500);
FlatList and its Items:
<FlatList
ref={(ref) => { this.flatListRef = ref; }}
horizontal = {true}
data={this.state.dataForCarousel}
renderItem={this._renderItem}
onScroll={this.handleScroll}
>
</FlatList>
_renderItem = (item) => {
let item1 = item;
var txt = item1.item;
console.log(txt);
var bgColor = item1.index % 2 == 0 ? 'red' : 'yellow';
return (
<TouchableOpacity
onPress={this.clickItem}
>
<View
style={{ flex: 1, alignItems: "center", justifyContent: "center",backgroundColor: bgColor, width: 80 }}>
<Text>{txt}</Text>
</View>
</TouchableOpacity>
)
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
[Please Help] Click Event on FlatList items not working during ...
I want the app to scroll through the list by itself automatically, but during my testing, when I use timer to call FlatList's...
Read more >React Native touchable opacity fails inside a ... - Stack Overflow
SOLVED The issue was that generating keys for each item in the flatlist using: keyExtractor={item => Math.floor(Math.random() * 1000000).
Read more >How to build a customized React Native activity indicator
Learn how to customize cross-platform activity indicators in React Native from scratch, with third-party libraries, and inbuilt APIs!
Read more >React Native Touchable Opacity Fails Inside A ... - ADocLib
React Native's FlatList component renders list items that can be renderItem requires a function that takes an item object as an input from ......
Read more >react native flatlist infinite scroll horizontal flatlist - You.com
The problem is, I would like to update the state so that a progress view shows the current step information (e.g. 'Step x...
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
Hey @malonguwa I was having trouble with the same thing and I found a workaround but its not the prettiest. In your onPress write a fat arrow function instead of the name of the function you want to execute. For example it should look like this:
onPress{() => {console.log(“sdf”)}} instead of
logIfPressed() { console.log(“sdf”) }
onPress{this.logIfPressed}
Of course this is messy but its the only workaround I found. Good luck!
@bd-arc Thanks for your time and effort! Really appreciate it! I think I make it work 😃
After I dump the RN’s own touchable component and implement my own touch event handler. I also set scrollToOffset animated prop to false and disabled timer in onResponderGrant.
I will try to implement the infinite auto scroll at the next step, cos in my case, I request a 2000 long array from server, and my idea is use another processedArray to hold the 20 or more fixed number of items from serverArray, and once I scroll to a certain position and then I will process the processedArray by taken different range of element from serverArray.
May I ask is this correct logic to implement infinite auto scroll ? cos I am increasing the FlatList’s contentOffsetX, and it will become to a really huge number if keep auto scrolling… is there any way I can do something about this ?
Thanks very much !!
Please refer to https://snack.expo.io/@lma/timerwithcarousel for the demo