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

Layout animation for FlatList #2674

Merged
merged 2 commits into from
Dec 3, 2021
Merged

Layout animation for FlatList #2674

merged 2 commits into from
Dec 3, 2021

Conversation

piaskowyk
Copy link
Member

@piaskowyk piaskowyk commented Dec 1, 2021

Description

The default implementation of FlatList wraps every item by custom component. This component hasn't got children's properties - in effect wrapping component hasn't the layout property with layout animation. It means the animation of the layout will not run because the "layout event" will be consumed by the wrapping component instead of his child.

How to use it

<Animated.FlatList // use Animated.FlatList instead of FlatList
  itemLayoutAnimation={Layout.springify()} // this animation will be apply as layout property for every list item
  data={itemsData} // as always
  renderItem={renderItem} // as always
/>

Fixes: #2664 #2424

Inspired by: @nicholash747 's #2424 (comment) - thanks! 🚀

Copy link
Member

@WoLewicki WoLewicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added 2 comments, if they do not introduce any new changes, I think it is ready to be merged 🎉

);
return (
<AnimatedFlatList
{...(restProps as any)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do the types don't work correctly here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because FlatList is generic. @jakub-gonet and I already had a hard time trying to get this working, I'd be surprised if there was a simple solution 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one comment inline. Looks good otherwise

itemLayoutAnimation,
...restProps
}) => {
const cellRenderer = React.useMemo(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to use memo here instead of just a simple method? Seems like updating layout animation won't have any effect given memo has empty array as dependencies now

@piaskowyk piaskowyk merged commit 4047392 into master Dec 3, 2021
@piaskowyk piaskowyk deleted the @piaskowyk/flatlist branch December 3, 2021 14:08
@braincore
Copy link

@piaskowyk When will there be a new beta release that includes this? I'm having trouble updating my Podfile to use this commit pre-release.

@piaskowyk
Copy link
Member Author

This PR is released today with stable 2.3.0

@@ -256,6 +257,10 @@ declare module 'react-native-reanimated' {
getNode(): ReactNativeScrollView;
}
export class Code extends Component<CodeProps> {}
export class FlatList extends Component<AnimateProps<FlatList>> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@piaskowyk this type definition is missing the generic type mentioned in issue 2769. Should probably be defined as:

export class FlatList<T = any> extends Component<AnimateProps<FlatListProps<T>>> {
  itemLayoutAnimation: ILayoutAnimationBuilder;
  getNode(): ReactNativeFlatList;
}

@CallumHemsley
Copy link

Is there any requirements to use this? Do children need to be Animated.View for example? @piaskowyk

@hadnet
Copy link

hadnet commented Oct 16, 2022

Is this available in Reanimated 3-rc?

github-merge-queue bot pushed a commit that referenced this pull request Jul 23, 2024
## Summary

This PR adds example usage of `itemLayoutAnimation` prop to the example
app and docs page with example recording and details.

## Example image of the added docs page

![Screenshot 2024-07-22 at 15 46
23](https://github.com/user-attachments/assets/bcb5667c-afb5-45b9-9c25-114f7b06e0c0)

## Related context

Related issue: #6278

Support for `multi-column` lists seems to be impossible to implement.
react-native adds additional wrapper component for each row and
re-renders list items in different rows when new items are added or
items are removed from the list. Because the parent of list items
changes and the layout animation cannot be applied to the wrapper that
is added to list rows, layout animations won't work for lists with
multiple columns.

At least, I didn't come up with any valid solution.

PR that adds support for `FlatList` items animations: #2674
What react-native does for mutli-column lists:
https://github.com/facebook/react-native/blob/2098806c2207f376027184329a7285913ef8d090/packages/react-native/Libraries/Lists/FlatList.js#L643

---------

Co-authored-by: Tomasz Żelawski <[email protected]>
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.

Layout transition is not working [2.3.0-alpha.3] Layout animation does not work inside FlatList
9 participants