question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

interpolateColor on <Animated.Text /> causes warning

See original GitHub issue

Description

When interpolating the color of an <Animated.Text> the following warning is displayed:

Warning: You are setting the style { color: ... } as a prop. You should nest it in a style object. E.g. { style: { color: ... } }

The animation works fine, however, the warning is annoying and I don’t want to ignore it as it may be an underlying issue of some sort.

Screenshots

IMG_0791

Steps To Reproduce

const colorMap = useSharedValue(0);

const animatedPlaceholderStyles = useAnimatedStyle(() => ({
      color: interpolateColor(
        colorMap.value,
        [0, 1, 2],
        [inactiveColor, activeColor, errorColor]
      ),
    }));

return (
      <Animated.View style={[styles.container, animatedContainerStyle]}>
        <Animated.Text
          style={[styles.placeholder, animatedPlaceholderStyles]}
          onLayout={handlePlaceholderLayout}
        >
          {placeholder}
        </Animated.Text>
      </Animated.View>
    );

Expected behavior

No warning with expected animation.

Actual behavior

Warning with expected animation.

Snack or minimal code example

See above.

Package versions

  • React: 16.13.1
  • React Native: 0.63.4
  • React Native Reanimated: 2.0.0-rc.0
  • NodeJS: 12.18.3

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
swansonteccommented, Aug 13, 2021

After many, many hours of debugging, I finally have some insight into what causes this!

The native Objective C / Java node manager uses a whitelist to determine which props it can set natively. There is a Javascript function called configureProps that is responsible for building this whitelist and sending it down to the native code. This needs to happen before any rendering takes place, or the native Objective C / Javascript code will treat everything as a JS prop, which causes this warning.

The tricky thing is that recent React Native versions load modules lazily by default. This means configureProps won’t have a chance to run until some poor animated component decides to render for the first time. By then, it’s too late. The native configureProps call will get batched up on the bridge, and won’t execute until after the render completes with an empty whitelist.

One super-hacky way to defeat the lazy module loading is to add code like this to the app’s index.js:

import Animated from 'react-native-reanimated'

Animated.addWhitelistedNativeProps({})

This forces react-native-reanimated to download the whitelist before the app has a chance to render anything.

1reaction
tomekzawcommented, Mar 24, 2021

@swushi Thank you for the detailed description. I have reproduced the issue on the example app from your repository (using Reanimated 2.0.0-rc.0) by commenting out the LogBox.ignore calls.

After bumping Reanimated to 2.0.0 to check if the problem persists on the latest release, I get the following error:

However, when I add your library as a dependency to our Reanimated 2 Example app, remove the LogBox.ignore calls from source code located in node_modules and then replace any existing screen with one of the showcase examples, there are no warnings or errors at all (in particular, color interpolation works great). This suggests that there might be either a problem with using Reanimated 2 on Expo or some issue specific to your example app.

Read more comments on GitHub >

github_iconTop Results From Across the Web

interpolateColors | React Native Reanimated
Maps an input value within a range to an output value within a color range. Example: const color = Animated.interpolateColors( ...
Read more >
Animated - React Native
The Animated library is designed to make animations fluid, powerful, and painless to build and maintain. Animated focuses on declarative ...
Read more >
Reanimated 2 - the new approach to creating animations in ...
Reanimated 2 - exploring the new API to creating animations in React ... View, Text} from 'react-native' import { StyleSheet, View, Alert } ......
Read more >
reactjs - React Native Animated: how to detect moment when ...
import Animated, { interpolateColor, useAnimatedStyle, ... the destination offset of the square - substitute whatever makes sense const ...
Read more >
Animations in React Native: Performance and Reason-about ...
While discussing building smooth animations and gestures with a ... The color of the text. color: interpolateColor( percentComplete, [0, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found