Skip to content

Implement React Native Reanimated V2 #13452

@Parveshdhull

Description

@Parveshdhull

Research issue for the implementation of react-native-reanimated v2

Summary:

Currently we are only using reanimated v1 API's and only version is bumped in package.json. We should Implement and use React Native Reanimated v2, at-least for newer animations for redesign.

Project Setup:

Configure project as per v2 installation guidelines and import API's.

Issues to fix

Issue 1:

useAnimatedStyle hook is one of the main element of reanimated v2. But when we use this hook like:

(reanimated/use-animated-style (fn [] #js {:width 100}))

It throws error:

Cannot convert undefined value to object

Screenshot:
Screenshot

this error is only shown in the older version, like 2.1.0, 2.2.0, etc. newer versions don't show errors due to this check, but the app just crashes with these logs

06-05 01:21:19.812 12779 12779 F DEBUG   : Abort message: 'JNI DETECTED ERROR IN APPLICATION: JNI GetObjectRefType called with pending exception java.lang.RuntimeException: Tried to synchronously call function {G__75911} from a different thread.
06-05 01:21:19.812 12779 12779 F DEBUG   : 
06-05 01:21:19.812 12779 12779 F DEBUG   : Occurred in worklet location: /home/parvesh_monu/Desktop/ArchFiles/React/status-react/node_modules/react-native-reanimated/src/reanimated2/hook/useAnimatedStyle.ts (492:18)
06-05 01:21:19.812 12779 12779 F DEBUG   : 
06-05 01:21:19.812 12779 12779 F DEBUG   : Possible solutions are:
06-05 01:21:19.812 12779 12779 F DEBUG   : a) If you want to synchronously execute this method, mark it as a Worklet
06-05 01:21:19.812 12779 12779 F DEBUG   : b) If you want to execute this method on the JS thread, wrap it using runOnJS
06-05 01:21:19.812 12779 12779 F DEBUG   :   at void com.swmansion.reanimated.AndroidErrorHandler.raise(java.lang.String) (AndroidErrorHandler.java:6)

Cause

This error occurs because useAnimatedStyle expects worklet as an argument (ref), while we are just passing a simple function.

While setting project we are also adding react-native-reanimated/plugin, which is supposed to convert the passed argument to worklet automatically (ref), which is not happening for us.

Issue 2

There is also another way to create worklet for reanimated v2. Using directives

function someWorklet(greeting) {
  'worklet';
  console.log("Hey I'm running on the UI thread");
}

This will also not work for clojure script, because even if I add "worklet" string, something like

(defn someWorklet [greeting]
    "worklet"
    (js/console.log "Hey I'm running on the UI thread"))

compiler will strip it out. It seems react-native-reanimated/plugin is more tailored toward javascript syntax and not working in cljs.

There is also a babel plugin available for using directive in cljs, but unfortunately it don't work in shadow-cljs.

Possible Solutions

  1. One workaround can be to not rely on directive and the library can just provide a function to manually create worklets. Function is already there just not yet completely implemented (ref). One drawback of this is we have to maintain fork branch for the implementation for now, because even if change gets merged in parent, we can't use that because after version 2.4.0, latest versions of library needs gradlePluginVersion=4.2.2 and gradle 6.
  2. Create or fix plugin so that directive also work for shadow-cljs. (?)

Related Refs:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions