Skip to content

Conversation

@Paduado
Copy link
Contributor

@Paduado Paduado commented Jul 31, 2024

Description

Adds recomputeViewableItems method to FlashList. Useful to imperatively trigger viewability calculations.

Reviewers’ hat-rack 🎩

  • Validate that calling the recomputeViewableItems method triggers the onViewableItemsChanged callback.

Checklist

@Paduado Paduado force-pushed the padua/recomputeViewableItems branch from 4ce269e to eec4cf7 Compare July 31, 2024 21:29
@Paduado Paduado requested a review from naqvitalha July 31, 2024 21:31
@Paduado Paduado force-pushed the padua/recomputeViewableItems branch from eec4cf7 to 643f9b2 Compare July 31, 2024 23:48
@naqvitalha naqvitalha merged commit fb8f63b into main Aug 2, 2024
@naqvitalha naqvitalha deleted the padua/recomputeViewableItems branch August 2, 2024 00:12
@zyestin
Copy link

zyestin commented Nov 27, 2024

Use recomputeViewableItems like this, it works!

import React, { useCallback, useEffect, useRef, useState } from "react";
import { View, Text } from "react-native";
import { FlashList, MasonryFlashList } from "@shopify/flash-list";

const DATA = [
  {
    title: "First Item",
  },
  {
    title: "Second Item",
  },
];

const DATA2 = [
  {
    title: "1- First Item",
  },
  {
    title: "2- Second Item",
  },
];

const MyList = () => {
  const [data, setData] = useState(DATA);
  const listRef = useRef();

  useEffect(() => {
    setTimeout(() => {
      setData(DATA2);
    }, 3000);
    setTimeout(() => {
      listRef.current?.recomputeViewableItems();
    }, 5000);
  }, []);

  const onViewableItemsChanged = useCallback(({ viewableItems, changed }) => {
    console.log("sa track::", viewableItems, changed);
  }, []);


  return (
    <FlashList
      ref={listRef}
      data={data}
      renderItem={({ item }) => <Text>{item.title}</Text>}
      estimatedItemSize={200}
      viewabilityConfig={{
        waitForInteraction: false,
        itemVisiblePercentThreshold: 50,
        minimumViewTime: 1000,
      }}
      onViewableItemsChanged={onViewableItemsChanged}
      contentContainerStyle={{ backgroundColor: "pink" }}
    />
  );
};

export default MyList;

But the MasonryFlashList I used in my project did not enjoy this benefit.

@zyestin
Copy link

zyestin commented Nov 27, 2024

I sumbited a PR here to fix "recomputeViewableItems" on MasonryFlashList.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants