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

Create recomputeViewableItems method #1296

Merged
merged 1 commit into from
Aug 2, 2024
Merged

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
11 checks passed
@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