Move ref ownership to the pager

This commit is contained in:
Dan Abramov
2023-11-10 02:14:04 +00:00
parent 342d18949f
commit 53aa7d14c0
3 changed files with 22 additions and 16 deletions
+4
View File
@@ -8,6 +8,7 @@ import Animated, {
useSharedValue, useSharedValue,
withTiming, withTiming,
runOnJS, runOnJS,
useAnimatedRef,
} from 'react-native-reanimated' } from 'react-native-reanimated'
import {Pager, PagerRef, RenderTabBarFnProps} from 'view/com/pager/Pager' import {Pager, PagerRef, RenderTabBarFnProps} from 'view/com/pager/Pager'
import {TabBar} from './TabBar' import {TabBar} from './TabBar'
@@ -20,6 +21,7 @@ interface PagerWithHeaderChildParams {
headerHeight: number headerHeight: number
onScroll: OnScrollCb onScroll: OnScrollCb
isScrolledDown: boolean isScrolledDown: boolean
scrollElRef: any /* TODO */
} }
export interface PagerWithHeaderProps { export interface PagerWithHeaderProps {
@@ -204,6 +206,7 @@ export const PagerWithHeader = React.forwardRef<PagerRef, PagerWithHeaderProps>(
function PagerItem( function PagerItem(
{headerHeight, isScrolledDown, onScroll, renderTab}: any /* TODO */, {headerHeight, isScrolledDown, onScroll, renderTab}: any /* TODO */,
) { ) {
const scrollElRef = useAnimatedRef()
if (renderTab == null) { if (renderTab == null) {
return null return null
} }
@@ -211,6 +214,7 @@ function PagerItem(
headerHeight, headerHeight,
isScrolledDown, isScrolledDown,
onScroll, onScroll,
scrollElRef,
}) })
} }
+9 -4
View File
@@ -1,5 +1,5 @@
import React, {useMemo, useCallback} from 'react' import React, {useMemo, useCallback} from 'react'
import {FlatList, StyleSheet, View, ActivityIndicator} from 'react-native' import {StyleSheet, View, ActivityIndicator} from 'react-native'
import {NativeStackScreenProps} from '@react-navigation/native-stack' import {NativeStackScreenProps} from '@react-navigation/native-stack'
import {useNavigation} from '@react-navigation/native' import {useNavigation} from '@react-navigation/native'
import {usePalette} from 'lib/hooks/usePalette' import {usePalette} from 'lib/hooks/usePalette'
@@ -342,13 +342,14 @@ export const ProfileFeedScreenInner = observer(
isHeaderReady={feedInfo?.hasLoaded ?? false} isHeaderReady={feedInfo?.hasLoaded ?? false}
renderHeader={renderHeader} renderHeader={renderHeader}
onCurrentPageSelected={onCurrentPageSelected}> onCurrentPageSelected={onCurrentPageSelected}>
{({onScroll, headerHeight, isScrolledDown}) => ( {({onScroll, headerHeight, isScrolledDown, scrollElRef}) => (
<FeedSection <FeedSection
ref={feedSectionRef} ref={feedSectionRef}
feed={feed} feed={feed}
onScroll={onScroll} onScroll={onScroll}
headerHeight={headerHeight} headerHeight={headerHeight}
isScrolledDown={isScrolledDown} isScrolledDown={isScrolledDown}
scrollElRef={scrollElRef}
/> />
)} )}
{({onScroll, headerHeight}) => ( {({onScroll, headerHeight}) => (
@@ -359,6 +360,7 @@ export const ProfileFeedScreenInner = observer(
headerHeight={headerHeight} headerHeight={headerHeight}
onToggleLiked={onToggleLiked} onToggleLiked={onToggleLiked}
onScroll={onScroll} onScroll={onScroll}
scrollElRef={scrollElRef}
/> />
)} )}
</PagerWithHeader> </PagerWithHeader>
@@ -386,14 +388,14 @@ interface FeedSectionProps {
onScroll: OnScrollCb onScroll: OnScrollCb
headerHeight: number headerHeight: number
isScrolledDown: boolean isScrolledDown: boolean
scrollElRef: any /* TODO */
} }
const FeedSection = React.forwardRef<SectionRef, FeedSectionProps>( const FeedSection = React.forwardRef<SectionRef, FeedSectionProps>(
function FeedSectionImpl( function FeedSectionImpl(
{feed, onScroll, headerHeight, isScrolledDown}, {feed, onScroll, headerHeight, isScrolledDown, scrollElRef},
ref, ref,
) { ) {
const hasNew = feed.hasNewLatest && !feed.isRefreshing const hasNew = feed.hasNewLatest && !feed.isRefreshing
const scrollElRef = React.useRef<FlatList>(null)
const onScrollToTop = useCallback(() => { const onScrollToTop = useCallback(() => {
scrollElRef.current?.scrollToOffset({offset: -headerHeight}) scrollElRef.current?.scrollToOffset({offset: -headerHeight})
@@ -437,6 +439,7 @@ const AboutSection = observer(function AboutPageImpl({
headerHeight, headerHeight,
onToggleLiked, onToggleLiked,
onScroll, onScroll,
scrollElRef,
}: { }: {
feedOwnerDid: string feedOwnerDid: string
feedRkey: string feedRkey: string
@@ -444,6 +447,7 @@ const AboutSection = observer(function AboutPageImpl({
headerHeight: number headerHeight: number
onToggleLiked: () => void onToggleLiked: () => void
onScroll: OnScrollCb onScroll: OnScrollCb
scrollElRef: any /* TODO */
}) { }) {
const pal = usePalette('default') const pal = usePalette('default')
const {_} = useLingui() const {_} = useLingui()
@@ -454,6 +458,7 @@ const AboutSection = observer(function AboutPageImpl({
return ( return (
<ScrollView <ScrollView
ref={scrollElRef}
scrollEventThrottle={1} scrollEventThrottle={1}
contentContainerStyle={{paddingTop: headerHeight}} contentContainerStyle={{paddingTop: headerHeight}}
onScroll={onScroll}> onScroll={onScroll}>
+9 -12
View File
@@ -1,11 +1,5 @@
import React, {useCallback, useMemo} from 'react' import React, {useCallback, useMemo} from 'react'
import { import {ActivityIndicator, Pressable, StyleSheet, View} from 'react-native'
ActivityIndicator,
FlatList,
Pressable,
StyleSheet,
View,
} from 'react-native'
import {useFocusEffect} from '@react-navigation/native' import {useFocusEffect} from '@react-navigation/native'
import {NativeStackScreenProps, CommonNavigatorParams} from 'lib/routes/types' import {NativeStackScreenProps, CommonNavigatorParams} from 'lib/routes/types'
import {useNavigation} from '@react-navigation/native' import {useNavigation} from '@react-navigation/native'
@@ -175,18 +169,20 @@ export const ProfileListScreenInner = observer(
isHeaderReady={list.hasLoaded} isHeaderReady={list.hasLoaded}
renderHeader={renderHeader} renderHeader={renderHeader}
onCurrentPageSelected={onCurrentPageSelected}> onCurrentPageSelected={onCurrentPageSelected}>
{({onScroll, headerHeight, isScrolledDown}) => ( {({onScroll, headerHeight, isScrolledDown, scrollElRef}) => (
<FeedSection <FeedSection
ref={feedSectionRef} ref={feedSectionRef}
scrollElRef={scrollElRef}
feed={feed} feed={feed}
onScroll={onScroll} onScroll={onScroll}
headerHeight={headerHeight} headerHeight={headerHeight}
isScrolledDown={isScrolledDown} isScrolledDown={isScrolledDown}
/> />
)} )}
{({onScroll, headerHeight, isScrolledDown}) => ( {({onScroll, headerHeight, isScrolledDown, scrollElRef}) => (
<AboutSection <AboutSection
ref={aboutSectionRef} ref={aboutSectionRef}
scrollElRef={scrollElRef}
list={list} list={list}
descriptionRT={list.descriptionRT} descriptionRT={list.descriptionRT}
creator={list.data ? list.data.creator : undefined} creator={list.data ? list.data.creator : undefined}
@@ -557,14 +553,14 @@ interface FeedSectionProps {
onScroll: OnScrollCb onScroll: OnScrollCb
headerHeight: number headerHeight: number
isScrolledDown: boolean isScrolledDown: boolean
scrollElRef: any /* TODO */
} }
const FeedSection = React.forwardRef<SectionRef, FeedSectionProps>( const FeedSection = React.forwardRef<SectionRef, FeedSectionProps>(
function FeedSectionImpl( function FeedSectionImpl(
{feed, onScroll, headerHeight, isScrolledDown}, {feed, scrollElRef, onScroll, headerHeight, isScrolledDown},
ref, ref,
) { ) {
const hasNew = feed.hasNewLatest && !feed.isRefreshing const hasNew = feed.hasNewLatest && !feed.isRefreshing
const scrollElRef = React.useRef<FlatList>(null)
const onScrollToTop = useCallback(() => { const onScrollToTop = useCallback(() => {
scrollElRef.current?.scrollToOffset({offset: -headerHeight}) scrollElRef.current?.scrollToOffset({offset: -headerHeight})
@@ -611,6 +607,7 @@ interface AboutSectionProps {
onScroll: OnScrollCb onScroll: OnScrollCb
headerHeight: number headerHeight: number
isScrolledDown: boolean isScrolledDown: boolean
scrollElRef: any /* TODO */
} }
const AboutSection = React.forwardRef<SectionRef, AboutSectionProps>( const AboutSection = React.forwardRef<SectionRef, AboutSectionProps>(
function AboutSectionImpl( function AboutSectionImpl(
@@ -624,13 +621,13 @@ const AboutSection = React.forwardRef<SectionRef, AboutSectionProps>(
onScroll, onScroll,
headerHeight, headerHeight,
isScrolledDown, isScrolledDown,
scrollElRef,
}, },
ref, ref,
) { ) {
const pal = usePalette('default') const pal = usePalette('default')
const {_} = useLingui() const {_} = useLingui()
const {isMobile} = useWebMediaQueries() const {isMobile} = useWebMediaQueries()
const scrollElRef = React.useRef<FlatList>(null)
const onScrollToTop = useCallback(() => { const onScrollToTop = useCallback(() => {
scrollElRef.current?.scrollToOffset({offset: -headerHeight}) scrollElRef.current?.scrollToOffset({offset: -headerHeight})