Skip to content
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

FlatList with sticky ListHeaderComponent, stickyHeaderIndices, onViewableItemsChanged gives incorrect viewableItems #31109

Closed
tt0mmy opened this issue Mar 6, 2021 · 9 comments
Labels
Component: FlatList Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@tt0mmy
Copy link

tt0mmy commented Mar 6, 2021

Description

When we use <FlatList /> with stickyHeaderIndices, viewabilityConfigCallbackPairs, and ListHeaderComponent together and when we scroll the list and onViewableItemsChanged is called, the viewableItems includes the items that are behind (not viewable) the sticky list header.

const viewabilityConfig = {
  minimumViewTime: 1000,
  waitForInteraction: true,
  itemVisiblePercentThreshold: 100,
}

const onViewableItemsChanged = React.useCallback(
  ({ viewableItems, changed }) => {
    console.log('viewableItems', viewableItems)
    console.log('changed', changed)
  },
  []
)

const viewabilityConfigCallbackPairs = React.useRef([
  { viewabilityConfig, onViewableItemsChanged },
])

<FlatList
  stickyHeaderIndices={[0]}
  viewabilityConfigCallbackPairs={viewabilityConfigCallbackPairs.current}
  ListHeaderComponent={() => <MyListHeader/>}
/>

React Native version:

"react-native": "0.63.4"

Steps To Reproduce

  1. drag the list so that the 2nd item is the first in the list
  2. observe the viewableItems
    image

Expected Results

I expect in viewableItems that the item with id 1 is not present and that the item with id 2 is the first in the list.

Snack, code example, screenshot, or link to a repository:

https://snack.expo.io/@tantommy/intelligent-churros

Related Issues:

@tt0mmy tt0mmy changed the title FlatList with sticky ListHeaderComponent, onViewableItemsChanged gives incorrect viewableItems FlatList with sticky ListHeaderComponent, stickyHeaderIndices, onViewableItemsChanged gives incorrect viewableItems Mar 6, 2021
@452MJ
Copy link

452MJ commented Mar 8, 2021

If you have such a need about sticky header, I think you can use SectionList to render it? Put the data of the flatList into the sections[0].data

function App() {
  const [list, setList] = useState([])
  const [sectionList, setSectionList] = useState([])

  useEffect(() => {
    setList(new Array(20).fill(0))
  }, [])
  useEffect(() => {
    setSectionList([
      {
        title: 'Sticky Tab',
        data: list,
      },
    ])
  }, [list])


  return (
      <SectionList
        stickySectionHeadersEnabled
        sections={sectionList}
        keyExtractor={(item, index) => item + index}
        renderItem={({ item }) => <ItemCompoment/>}
        renderSectionHeader={({ section: { title } }) => <StickyCompoment />}
      />
  )
}

@tt0mmy
Copy link
Author

tt0mmy commented Mar 8, 2021

If you have such a need about sticky header, I think you can use SectionList to render it? Put the data of the flatList into the sections[0].data

function App() {
  const [list, setList] = useState([])
  const [sectionList, setSectionList] = useState([])

  useEffect(() => {
    setList(new Array(20).fill(0))
  }, [])
  useEffect(() => {
    setSectionList([
      {
        title: 'Sticky Tab',
        data: list,
      },
    ])
  }, [list])


  return (
      <SectionList
        stickySectionHeadersEnabled
        sections={sectionList}
        keyExtractor={(item, index) => item + index}
        renderItem={({ item }) => <ItemCompoment/>}
        renderSectionHeader={({ section: { title } }) => <StickyCompoment />}
      />
  )
}

@452MJ I appreciate the suggestion. I just tried it and it behaves the same as the <FlatList/> implementation. The workaround I have now is to take the <ListHeader /> and put it above <FlatList /> like this:

<>
    <ListHeader/>
    <FlatList
      viewabilityConfigCallbackPairs={viewabilityConfigCallbackPairs.current}
    />
</>

@452MJ
Copy link

452MJ commented Mar 9, 2021

<>
    <FlatList
      viewabilityConfigCallbackPairs={viewabilityConfigCallbackPairs.current}
    />
    <ListHeader/>
</>

If you don't use ZIndex, components are layered. So the ListHeader will naturally be underneath the FlatList. It just looks weird in code

@tt0mmy
Copy link
Author

tt0mmy commented Mar 9, 2021

<>
    <FlatList
      viewabilityConfigCallbackPairs={viewabilityConfigCallbackPairs.current}
    />
    <ListHeader/>
</>

If you don't use ZIndex, components are layered. So the ListHeader will naturally be underneath the FlatList. It just looks weird in code

I'm not sure i understand what you mean. In your quote, you have the <ListHeader /> placed below the <FlatList /> but how I have it, it's above.

When using <FlatList /> with the props ListHeaderComponent, and stickyHeaderIndices, the scroll area is the full height of the scree. When I don't use those props and place the <ListHeader /> outside (above) the <FlatList />, then the scrollable area is from the bottom of <ListHeader/> to the bottom of the screen. When we do this, viewableItems seem to be called with the correct items.
image

@chrisglein chrisglein added Component: FlatList Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. and removed Needs: Triage 🔍 labels Mar 9, 2021
@chrisglein
Copy link

Is it just off by the one that's underneath the header? (like if you scroll down more is it including both 1 and 2?) Or are there more offscreen items being included as "viewable"? There's a question about how many are realized versus how many are visible. @NickGerleman hadn't you done some digging into what it'd look like to have this more customizable?

@tt0mmy
Copy link
Author

tt0mmy commented Mar 9, 2021

Is it just off by the one that's underneath the header? (like if you scroll down more is it including both 1 and 2?) Or are there more offscreen items being included as "viewable"? There's a question about how many are realized versus how many are visible. @NickGerleman hadn't you done some digging into what it'd look like to have this more customizable?

If i scroll enough so that items 1 and 2 are behind the <ListHeader />, both of them are still being reported as viewable

@spaceye
Copy link

spaceye commented Aug 28, 2021

Applicable to FlatList with ListHeaderComponent

Funny thing is that the fix was there.

But currently — as of 0.64 — it is back to square one.

This feature is really important but such omissions render it useless when it comes to FlatLists with headers.

One thing to notice — it won't take into account whatever margin / padding you may have set via contentContainerStyle prop. There's a workaround for that to drop those off into your header component but it still feels like.. a workaround.

@stale
Copy link

stale bot commented Jan 9, 2022

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 9, 2022
@github-actions
Copy link

github-actions bot commented Feb 3, 2022

This issue was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions bot closed this as completed Feb 3, 2022
@facebook facebook locked as resolved and limited conversation to collaborators Feb 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Component: FlatList Issue: Author Provided Repro This issue can be reproduced in Snack or an attached project. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

4 participants