Skip to content

fix: layout recalculation issue when data changes #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions example/src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { StyleSheet, Text, View } from 'react-native'
import React, { useState } from 'react'
import { StyleSheet, Text, View, Button } from 'react-native'
import Steve from 'react-native-steve'

const topics = [
Expand Down Expand Up @@ -62,6 +62,7 @@ const topics = [
]

export default function App() {
const [data, setData] = useState(topics)
const {
topicContainer,
topicText,
Expand All @@ -84,19 +85,27 @@ export default function App() {
)
}

const handleUpdateDataButtonClick = () => {
setData(topics.filter((_, index) => index < Math.random() * 10).reverse())
}

return (
<View style={container}>
<Text style={title}>
{'TOPICS TO EXPLORE'}
</Text>
<Steve
{...{ data }}
isRTL={false}
data={topics}
itemSpacing={10}
renderItem={renderTopic}
itemStyle={{ margin: 5 }}
containerStyle={steveContainer}
keyExtractor={item => item.text}/>

<Button
title={'Update Data'}
onPress={handleUpdateDataButtonClick}/>
</View>
)
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"react-fast-compare": "^3.2.0"
},
"devDependencies": {
"@commitlint/config-conventional": "^11.0.0",
"@react-native-community/eslint-config": "^2.0.0",
Expand Down
36 changes: 29 additions & 7 deletions src/Steve.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import React, { useRef, useState } from 'react'
import React, { useRef, useEffect } from 'react'
import Animated, {
useAnimatedGestureHandler,
useSharedValue,
withSpring,
cancelAnimation,
useAnimatedStyle,
withDecay
withDecay,
withTiming
} from 'react-native-reanimated'
import { Dimensions } from 'react-native'
import { Dimensions, LayoutAnimation } from 'react-native'
import { PanGestureHandler } from 'react-native-gesture-handler'
import isEqual from 'react-fast-compare'
import { useObjectState } from 'hooks'

const { width: screenWidth } = Dimensions.get('window')
const TIMING_ANIMATION_DURATION = 300

const getContainerHorizontalSpacing = style => {
const {
Expand All @@ -28,8 +32,11 @@ const getContainerHorizontalSpacing = style => {
}

export const Steve = ({ data, renderItem, keyExtractor, containerStyle, isRTL, itemStyle }) => {
const [{ dataCache, itemLayouts }, setState] = useObjectState({
dataCache: data,
itemLayouts: {}
})
const itemLayoutsCache = useRef({})
const [itemLayouts, setItemLayouts] = useState({})
const containerHorizontalSpacing = getContainerHorizontalSpacing(containerStyle)
const translateX = useSharedValue(0)
const rtlStyle = isRTL ? { flexDirection: 'row-reverse' } : {}
Expand Down Expand Up @@ -98,10 +105,25 @@ export const Steve = ({ data, renderItem, keyExtractor, containerStyle, isRTL, i
)
}
}
})
}, [itemLayouts])

useEffect(() => {
(async () => {
if (!isEqual(dataCache, data)) {
translateX.value = withTiming(0)
LayoutAnimation.easeInEaseOut()
await new Promise(resolve => setTimeout(resolve, TIMING_ANIMATION_DURATION))
setState({
dataCache: data,
itemLayouts: {}
})
itemLayoutsCache.current = {}
}
})()
}, [data, dataCache])

const Items = () => {
return data.map((item, index) => {
return dataCache.map((item, index) => {
const itemKey = keyExtractor(item, index)
return (
<Item
Expand Down Expand Up @@ -174,7 +196,7 @@ export const Steve = ({ data, renderItem, keyExtractor, containerStyle, isRTL, i
accumulator.sumWidthOfLayer[current.layout.y] += current.layout.width + spacingBetweenItems
return accumulator
}, itemLayoutsCache.current)
setItemLayouts(itemLayoutsCache.current)
setState({ itemLayouts: itemLayoutsCache.current })
}

const getSpacingBetweenItems = () => {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'hooks/useObjectState'
3 changes: 3 additions & 0 deletions src/hooks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "hooks"
}
6 changes: 6 additions & 0 deletions src/hooks/useObjectState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useState } from 'react'

export const useObjectState = initialState => {
const [state, setState] = useState(initialState)
return [state, state => setState(prevState => ({ ...prevState, ...state }))]
}
3 changes: 3 additions & 0 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "src"
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7818,6 +7818,11 @@ react-devtools-core@^4.6.0:
shell-quote "^1.6.1"
ws "^7"

react-fast-compare@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-3.2.0.tgz#641a9da81b6a6320f270e89724fb45a0b39e43bb"
integrity sha512-rtGImPZ0YyLrscKI9xTpV8psd6I8VAtjKCzQDlzyDvqJA8XOW78TXYQwNRNd8g8JZnDu8q9Fu/1v4HPAVwVdHA==

react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
Expand Down