react-native-reanimated: [3.16.1] NativeWind classes not being applied on Animated.View

Description

React-Native-Reanimated (at 3.10.1) was working with NativeWind to correctly apply classes to an Animated.View.

After upgrading Reanimated to 3.16.1 NW classnames were no longer getting applied to an Animated.View.

I opened a ticket with NativeWind too, so both teams could be notified.

Working Project Details Not-Working Project Details
Expo 52.0.0-preview.18 Expo 52.0.0-preview.18
React NativeReanimated 3.10.1 React NativeReanimated at 3.16.1
NativeWind at 4.1.21 NativeWind at 4.1.21
rnreanimated 3.10.1 rnreanimated 3.16.1
Screenshot 2024-11-04 at 10 18 46 AM Screenshot 2024-11-04 at 10 20 16 AM

Steps to reproduce

  1. Create a repo with npx create-expo-stack@latest --nativewind
  2. Upgrade Reanimated from 3.10.1 to 3.16.1
  3. Add classNames to an animated view. (Should work, but does not)
  4. Downgrade Reanimated to 3.10.1. (Works as expected)

Snack or a link to a repository

https://github.com/amandaharlin/react-native-reanimated-nativewind-classname-repro

Reanimated version

3.16.1

React Native version

0.76.1

Platforms

Web

JavaScript runtime

None

Workflow

Expo Go

Architecture

None

Build type

Debug app & dev bundle

Device

None

Device model

No response

Acknowledgements

Yes

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 29 (6 by maintainers)

Commits related to this issue

Most upvoted comments

This issue is an upstream bug with react-native-builder-bob https://github.com/callstack/react-native-builder-bob/issues/678

I’ve the issue isn’t addressed soon then I’ll probably adjust the nativewind babel plugin to work around it

Another workaround is to use

const MyAnimatedView = cssInterop(Animated.View, { className: "style" })

<MyAnimatedView />

@marklawlor I submitted a pull request in react-native-builder-bob to address this issue. We’ll release 3.16.3 with these changes. However, you should somehow migrate from relying on classic jsx runtime as automatic is going to be the default soon.

@marklawlor @amandaharlin Thanks for troubleshooting this!

I wanted to provide a quick update here, firstly

import Animated from 'react-native-reanimated';
import type { ViewProps } from 'react-native';
import type { AnimatedProps } from 'react-native-reanimated';
import { cssInterop } from 'nativewind';
import { cn } from '~/lib/utils';

//move to here
const InteropAnimatedView = cssInterop(Animated.View, { className: 'style' }); 

export const AnimatedView = ({ className, ...animatedViewProps }: AnimatedProps<ViewProps>) => {
  //const InteropAnimatedView = cssInterop(Animated.View, { className: 'style' }); 
  return <InteropAnimatedView {...animatedViewProps} className={cn(className)} />;
};

this works just fine after testing, and should avoid the performance pitfall you mention, so I’ve updated my code snippet

That said, i’ve not yet updated my reproduction repo, but the mobile behavior has actually degraded more from the time I posted this issue. I had been using

import Animated from 'react-native-reanimated';
export const AnimatedView = Animated.View;

and this was working beforehand on the mobile platforms. Now, when I use that code or if I use the css Interop approach for mobile, neither one is working. While trying to zero in on “what changed”, I can see that I haven’t changed nativewind or react-native-reanimated but expo did update. I can’t see immediately how that could cause this issue, so I am still investigating. That said, last week iOS and Android were working (and only web needed to be shimmed), now mobile doesn’t work at all, but the fix upthread for web does still work great for web builds, and seems to have no effect on mobile.

To get around this I’ve fallen back to just specifying css properties with the standard JSON syntax on the style prop for now.

Yes, this will definitely land in Reanimated 4.

@kaumac @Babettestam I totally forgot what the issue was originally about and how it was resolved (or rather not resolved).

I have created a version of Reanimated 3.17.1 that should address the underlying problem, could you try it and see if it works in your setup?

react-native-reanimated-3.17.1-jsx.tgz

Can somebody reopen the issue, since the problem is not solved?

cssInterop returns a new component. Calling it during render will always cause a remount of that component (as your rendering something different everytime)

Thanks! That works great. Here’s my full cross-platform component in typescript.

animated-view.web.tsx

import Animated from 'react-native-reanimated';
import type { ViewProps } from 'react-native';
import type { AnimatedProps } from 'react-native-reanimated';
import { cssInterop } from 'nativewind';
import { cn } from '~/lib/utils';

const InteropAnimatedView = cssInterop(Animated.View, { className: 'style' }); 

export const AnimatedView = ({ className, ...animatedViewProps }: AnimatedProps<ViewProps>) => {
  return <InteropAnimatedView {...animatedViewProps} className={cn(className)} />;
};

animated-view.tsx

import Animated from 'react-native-reanimated';

export const AnimatedView = Animated.View;

utils.tsx

import type { ClassValue } from 'clsx';
import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

implementation.tsx

import Animated, { useAnimatedStyle } from 'react-native-reanimated';
import { AnimatedView } from '~/components/ui/animated-view';

const viewStyle = useAnimatedStyle(() => { 
/// interpolate here
}) 

<AnimatedView style={viewStyle} className="flex items-center justify-center">
  <View/>
</AnimatedView>

I’m using this with expo. It was working correctly on iOS and Android, this code snippet implements the change for the web build. With the .web switch, this puts them at feature parity.

Would you like for this to be the official way to use RNReanimated and NW? I can make a PR to the NW docs, which would close this issue.

This issue is caused by how react-native-reanimated builds in 3.1.16.

NativeWind relies on a jsx transform (turning <View> -> jsx(View), where it needs to use the jsx function from NativeWind.

3.1.16 is being published to NPM with the jsx function being hardcoded to react/jsx-runtime. This also means that react-native-reanimated will not work with libraries like @emotion/native which relies on the jsx transform.

I’m not actually sure why this is happeneding, as the tsconfig.json#jsx is correctly set to react-native, which should preserve the JSX.

A work around is to simply not use Animated.View. You can just use a View and add an animation class to it like animate-none, and NativeWind will turn your View into an Animated.View