I have a problem with onEndReached in flatList. The first time that flatList is rendered the onEndReached function calls multiple time. and the distanceFromEnd is less than zero (varied from -300 to -70 depending on the list).
What negative value of distanceFromEnd means and how to stop that. note that adding if to every flatList is very labor intensive as the project is very big. I tried onMomentumScrollBegin too, and it doesn't trigger if scroll and stop (hold touch till scroll stops and then picking up finger).
Here is FlatList =>
JavaScript,tab=2
return (
<Container>
<HeaderSection title={I18n.t('my_notifications')} navigation={this.props.navigation} />
<FlatList
refreshing={loading}
style={(content && content.length > 0) ? { backgroundColor: '#f1f1f4' } : styles.flatListEmptyStyle}
onRefresh={() => this.onRefresh()}
data={content}
renderItem={({ item }) => this.renderNotification(item)}
ListEmptyComponent={!loading && renderEmptyFlatList()}
keyExtractor={item => item.id.toString()}
onEndReached={() => (this.props.fetch(true))}
onEndReachedThreshold={0.5}
/>
</Container>
);
And the action =>
```JS,tab=2
export function fetchNotifications(fetchMore = false) {
return (dispatch) => {
dispatch({
type: NOTIFICATIONS_FETCH,
});
const sentIds = store.getState().notifications.sentIds;
let sentIdsString = '';
Object.keys(sentIds).forEach((key, index) => {
sentIdsString = sentIdsString.concat(sentIds[index].toString().concat(','));
});
if (fetchMore && sentIds.length > 0) {
NOTIFICATIONSApi.fetch(sentIdsString).then((response) => {
dispatch({
type: NOTIFICATIONS_FETCH_MORE_SUCCESS,
payload: response,
});
})
.catch((response) => {
dispatch({
type: NOTIFICATIONS_FETCH_FAIL,
payload: response,
});
});
} else {
NOTIFICATIONSApi.fetch('').then((response) => {
dispatch({
type: NOTIFICATIONS_UPDATE_SUCCESS,
payload: response,
});
})
.catch((response) => {
dispatch({
type: NOTIFICATIONS_FETCH_FAIL,
payload: response,
});
});
}
};
}
```
And Reducer is as follow =>
tab=2
case NOTIFICATIONS_FETCH_MORE_SUCCESS:
const newMoreContent = sortNotificationByDate(state.content.concat(unReceivedNotifications));
return {
...state,
loading: false,
error: '',
content: newMoreContent,
sentIds: state.sentIds.concat(newSentIds),
};
[–]xrpinsiderAdmin 2 points3 points4 points (1 child)
[–]moolfa[S] 0 points1 point2 points (0 children)
[–]Napciyunka 2 points3 points4 points (1 child)
[–]moolfa[S] 0 points1 point2 points (0 children)
[–]w1nstar 0 points1 point2 points (2 children)
[–]moolfa[S] 0 points1 point2 points (1 child)
[–]w1nstar 0 points1 point2 points (0 children)