Skip to content

Commit

Permalink
fix: check given data length before accessing with indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
tsdmrfth committed Apr 14, 2021
1 parent c0e9b89 commit 822e4dd
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Steve.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,7 @@ export const Steve = ({ data, renderItem, keyExtractor, containerStyle, isRTL, i
.values(itemLayoutsCache.current)
.reduce((accumulator, current, index) => {
if (index === 0) {
const firstIndex = isRTL ? 1 : 0
const secondIndex = isRTL ? 0 : 1
const firstKey = keyExtractor(data[firstIndex], firstIndex)
const secondKey = keyExtractor(data[secondIndex], secondIndex)
const firstItem = itemLayoutsCache.current[firstKey]
const secondItem = itemLayoutsCache.current[secondKey]
spacingBetweenItems = secondItem.layout.x - firstItem.layout.width - firstItem.layout.x
spacingBetweenItems = getSpacingBetweenItems()
}

if (!accumulator.sumWidthOfLayer) {
Expand All @@ -183,6 +177,20 @@ export const Steve = ({ data, renderItem, keyExtractor, containerStyle, isRTL, i
setItemLayouts(itemLayoutsCache.current)
}

const getSpacingBetweenItems = () => {
if (data.length < 2) {
return 0
}

const firstIndex = isRTL ? 1 : 0
const secondIndex = isRTL ? 0 : 1
const firstKey = keyExtractor(data[firstIndex], firstIndex)
const secondKey = keyExtractor(data[secondIndex], secondIndex)
const firstItem = itemLayoutsCache.current[firstKey]
const secondItem = itemLayoutsCache.current[secondKey]
return secondItem.layout.x - firstItem.layout.width - firstItem.layout.x
}

return (
<PanGestureHandler
activeOffsetX={[-10, 10]}
Expand Down

0 comments on commit 822e4dd

Please sign in to comment.