react-native: [New Architecture] LayoutAnimation Breaking

Description

After migrating to new architecture, the animation configured with LayoutAnimation are behaving differently, we can observe a kind of jerk in the animation. Attached the videos and repo for reproduction of the issue.

Also check in slow motion with simulator, eventually the views are not adopting the slow animation and breaking.

React Native Version

0.72.3

Output of npx react-native info

System: OS: macOS 13.4.1 CPU: (12) arm64 Apple M2 Pro Memory: 312.44 MB / 32.00 GB Shell: version: “5.9” path: /bin/zsh Binaries: Node: version: 16.20.0 path: /usr/local/bin/node Yarn: version: 1.22.19 path: /opt/homebrew/bin/yarn npm: version: 8.19.4 path: /usr/local/bin/npm Watchman: version: 2023.06.26.00 path: /opt/homebrew/bin/watchman Managers: CocoaPods: version: 1.12.1 path: /Users/ravindragupta/.rbenv/shims/pod SDKs: iOS SDK: Platforms: - DriverKit 22.4 - iOS 16.4 - macOS 13.3 - tvOS 16.4 - watchOS 9.4 Android SDK: Not Found IDEs: Android Studio: 2022.2 AI-222.4459.24.2221.10121639 Xcode: version: 14.3.1/14E300c path: /usr/bin/xcodebuild Languages: Java: version: 11.0.19 path: /usr/bin/javac Ruby: version: 3.0.4 path: /Users/ravindragupta/.rbenv/shims/ruby npmPackages: “@react-native-community/cli”: Not Found react: installed: 18.2.0 wanted: ^18.2.0 react-native: installed: 0.72.3 wanted: 0.72.3 react-native-macos: Not Found npmGlobalPackages: “react-native”: Not Found Android: hermesEnabled: false newArchEnabled: true iOS: hermesEnabled: false newArchEnabled: true

Steps to reproduce

Use LayoutAnimation with New Architecture (Fabric) and observe issues with animation.

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

Reproducible Demo App: https://github.com/ravindraguptacapgemini/reproducer-react-native-maps-events/tree/layout-animation-issue

Videos and Screenshot:

https://github.com/facebook/react-native/assets/97147467/d9e66cc5-0fd5-4881-91d9-dd9b684aba97

https://github.com/facebook/react-native/assets/97147467/b5b752b5-7d02-412d-a61d-7a8a82e6dbd2

New Architecture Animation Breaking

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 5
  • Comments: 25 (11 by maintainers)

Most upvoted comments

The problem here is with mixing JS and native side animations and the view flattening on top of that. When there is an animation defined on JS side (like the one in the reproducer with useNativeDriver set to false) it sends properties updates to cpp which causes creation of update mutations for the corresponding animated view. In the provided repro there is also a LayoutAnimation configured to run on the next layout alongside the JS animation. It’s registered as the inflightAnimation right before the first mutation update from the JS animation. So, when the update finally occurs, the algorithm will check for the potential conflict in the current layout animation with the mutation update in getAndEraseConflictingAnimations. One of the requirements for the conflict to happen is that the animated node (from the layout animation) is mutated directly. Even though in the above repro JS animations are updating background of the Animated.View and the layout animation update the bottom margin of its parent, the layout and JS animations are targeted at the same node, because of the view flattening, which causes those conflicts.

The solution to this problem is quite simple as we can set collapsable: false on the parent of the Animated.View to prevent the view flattening. In some cases the issue may not be registered at first, because some styles like: opacity, shadows or transforms prevent view flattening which sort of solves the problem, but changing these styles dynamically may reintroduce it.

<div align="center"> <video src="https://github.com/user-attachments/assets/e8b4b741-3d6e-4814-925a-547180bc2610" width="400" /> </div>

from the video it looks like the animation is not working at all and it only shows first and last frame.

Debug -> Slow Animation unfortunately won’t work out of the box. The trouble is, the animation is not driven by CoreAnimation framework on iOS. The New Architecture has a cross-platform implementation and does not use animation primitives that host platforms offer.