To use React Native's LayoutAnimation with FlashList, you must call the prepareForLayoutAnimationRender() instance method on your FlashList reference before calling LayoutAnimation.configureNext(). Additionally, you must provide a keyExtractor prop to your FlashList component to ensure elements are uniquely identified for the animation.
Note: LayoutAnimation is experimental on Android, and stability cannot be guaranteed when used with FlashList on that platform.
import React, { useRef, useState } from "react";
import { View, Text, Pressable, LayoutAnimation } from "react-native";
import { FlashList } from "@shopify/flash-list";
const List = () => {
const [data, setData] = useState([1, 2, 3, 4, 5]);
const list = useRef<FlashList<number> | null>(null);
const removeItem = (item: number) => {
setData(
data.filter((dataItem) => {
return dataItem !== item;
})
);
// This must be called before `LayoutAnimation.configureNext` in order for the animation to run properly.
list.current?.prepareForLayoutAnimationRender();
// After removing the item, we can start the animation.
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
};
const renderItem = ({ item }: { item: number }) => {
return (
<Pressable
onPress={() => {
removeItem(item);
}}
>
<View>
<Text>Cell Id: {item}</Text>
</View>
</Pressable>
);
};
return (
<FlashList
// Saving reference to the `FlashList` instance to later trigger `prepareForLayoutAnimationRender` method.
ref={list}
// This prop is necessary to uniquely identify the elements in the list.
keyExtractor={(item: number) => {
return item.toString();
}}
renderItem={renderItem}
data={data}
/>
);
};
export default List;