Optimize a bit

This commit is contained in:
Dan Abramov
2023-12-13 05:20:40 +00:00
parent a4473c91b7
commit 0192031f28
+16 -10
View File
@@ -1,8 +1,8 @@
import React, {useState} from 'react' import React, {startTransition} from 'react'
import {FlatListProps} from 'react-native' import {FlatListProps} from 'react-native'
import {FlatList_INTERNAL} from './Views' import {FlatList_INTERNAL} from './Views'
import {useScrollHandlers} from '#/lib/ScrollContext' import {useScrollHandlers} from '#/lib/ScrollContext'
import {runOnJS} from 'react-native-reanimated' import {runOnJS, useSharedValue} from 'react-native-reanimated'
import {useAnimatedScrollHandler} from '#/lib/hooks/useAnimatedScrollHandler_FIXED' import {useAnimatedScrollHandler} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
export type ListMethods = FlatList_INTERNAL export type ListMethods = FlatList_INTERNAL
@@ -17,27 +17,33 @@ function ListImpl<ItemT>(
{onScrolledDownChange, ...props}: ListProps<ItemT>, {onScrolledDownChange, ...props}: ListProps<ItemT>,
ref: React.Ref<ListMethods>, ref: React.Ref<ListMethods>,
) { ) {
const [isScrolledDown, setIsScrolledDown] = useState(false) const isScrolledDown = useSharedValue(false)
const scrollHandlers = useScrollHandlers() const contextScrollHandlers = useScrollHandlers()
function handleScrolledDownChange(didScrollDown: boolean) { function handleScrolledDownChange(didScrollDown: boolean) {
setIsScrolledDown(didScrollDown) startTransition(() => {
onScrolledDownChange?.(didScrollDown) onScrolledDownChange?.(didScrollDown)
})
} }
const scrollHandler = useAnimatedScrollHandler({ const scrollHandler = useAnimatedScrollHandler({
onBeginDrag(e, ctx) { onBeginDrag(e, ctx) {
scrollHandlers.onBeginDrag?.(e, ctx) contextScrollHandlers.onBeginDrag?.(e, ctx)
}, },
onEndDrag(e, ctx) { onEndDrag(e, ctx) {
scrollHandlers.onEndDrag?.(e, ctx) contextScrollHandlers.onEndDrag?.(e, ctx)
}, },
onScroll(e, ctx) { onScroll(e, ctx) {
contextScrollHandlers.onScroll?.(e, ctx)
const didScrollDown = e.contentOffset.y > SCROLLED_DOWN_LIMIT const didScrollDown = e.contentOffset.y > SCROLLED_DOWN_LIMIT
if (isScrolledDown !== didScrollDown) { if (
isScrolledDown.value !== didScrollDown &&
onScrolledDownChange != null
) {
isScrolledDown.value = didScrollDown
runOnJS(handleScrolledDownChange)(didScrollDown) runOnJS(handleScrolledDownChange)(didScrollDown)
} }
scrollHandlers.onScroll?.(e, ctx)
}, },
}) })