Skip to content

Commit

Permalink
fix: initial scroll position sometimes wrong on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
andreialecu committed May 19, 2022
1 parent f3e90b9 commit e349941
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
14 changes: 12 additions & 2 deletions src/FlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useAfterMountEffect,
useChainCallback,
useCollapsibleStyle,
useConvertAnimatedToValue,
useScrollHandlerY,
useSharedAnimatedRef,
useTabNameContext,
Expand Down Expand Up @@ -77,9 +78,17 @@ function FlatListImpl<R>(
[progressViewOffset, refreshControl]
)

const memoContentInset = React.useMemo(() => ({ top: contentInset.value }), [
contentInset.value,
const contentInsetValue = useConvertAnimatedToValue(contentInset)

const memoContentInset = React.useMemo(() => ({ top: contentInsetValue }), [
contentInsetValue,
])

const memoContentOffset = React.useMemo(
() => ({ x: 0, y: -contentInsetValue }),
[contentInsetValue]
)

const memoContentContainerStyle = React.useMemo(
() => [
_contentContainerStyle,
Expand All @@ -103,6 +112,7 @@ function FlatListImpl<R>(
onContentSizeChange={scrollContentSizeChangeHandlers}
scrollEventThrottle={16}
contentInset={memoContentInset}
contentOffset={memoContentOffset}
automaticallyAdjustContentInsets={false}
refreshControl={memoRefreshControl}
// workaround for: https://github.com/software-mansion/react-native-reanimated/issues/2735
Expand Down
15 changes: 12 additions & 3 deletions src/ScrollView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useAfterMountEffect,
useChainCallback,
useCollapsibleStyle,
useConvertAnimatedToValue,
useScrollHandlerY,
useSharedAnimatedRef,
useTabNameContext,
Expand Down Expand Up @@ -89,10 +90,17 @@ export const ScrollView = React.forwardRef<
[progressViewOffset, refreshControl]
)

const memoContentInset = React.useMemo(
() => ({ top: contentInset.value }),
[contentInset.value]
const contentInsetValue = useConvertAnimatedToValue(contentInset)

const memoContentInset = React.useMemo(() => ({ top: contentInsetValue }), [
contentInsetValue,
])

const memoContentOffset = React.useMemo(
() => ({ x: 0, y: -contentInsetValue }),
[contentInsetValue]
)

const memoContentContainerStyle = React.useMemo(
() => [
_contentContainerStyle,
Expand All @@ -114,6 +122,7 @@ export const ScrollView = React.forwardRef<
onContentSizeChange={scrollContentSizeChangeHandlers}
scrollEventThrottle={16}
contentInset={memoContentInset}
contentOffset={memoContentOffset}
automaticallyAdjustContentInsets={false}
refreshControl={memoRefreshControl}
// workaround for: https://github.com/software-mansion/react-native-reanimated/issues/2735
Expand Down
14 changes: 12 additions & 2 deletions src/SectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useAfterMountEffect,
useChainCallback,
useCollapsibleStyle,
useConvertAnimatedToValue,
useScrollHandlerY,
useSharedAnimatedRef,
useTabNameContext,
Expand Down Expand Up @@ -78,9 +79,17 @@ function SectionListImpl<R>(
[progressViewOffset, refreshControl]
)

const memoContentInset = React.useMemo(() => ({ top: contentInset.value }), [
contentInset.value,
const contentInsetValue = useConvertAnimatedToValue(contentInset)

const memoContentInset = React.useMemo(() => ({ top: contentInsetValue }), [
contentInsetValue,
])

const memoContentOffset = React.useMemo(
() => ({ x: 0, y: -contentInsetValue }),
[contentInsetValue]
)

const memoContentContainerStyle = React.useMemo(
() => [
_contentContainerStyle,
Expand All @@ -104,6 +113,7 @@ function SectionListImpl<R>(
onContentSizeChange={scrollContentSizeChangeHandlers}
scrollEventThrottle={16}
contentInset={memoContentInset}
contentOffset={memoContentOffset}
automaticallyAdjustContentInsets={false}
refreshControl={memoRefreshControl}
// workaround for: https://github.com/software-mansion/react-native-reanimated/issues/2735
Expand Down

0 comments on commit e349941

Please sign in to comment.