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.

v2 How does one use color interpolation and withTiming hook

See original GitHub issue

Description

I am using Android Studio’s adb to test my app. As per code, when the button is clicked, i would like to change the backgroundColor of the View “interpolate-ly” from tomato to teal.

  1. Using the code directly below will not change the color, but when Fast Reload is initiated from my code editor, the color did change to teal.
  2. Using the commented backgroundColorInterpolation to style={[anyOtherStyle, backgroundColorInterpolation]} will crash my app upon entering the screen

Is that not how it works or has i been implementing them the wrong way?

Code

import {  Button,  } from 'react-native';
import Animated ,{
        useAnimatedStyle,
	useSharedValue,
	withTiming,
	interpolateColors
} from 'react-native-reanimated';

function testScreen(){

	const colorAnim = useSharedValue(0)
    
	const backgroundColorInterpolation = interpolateColors(colorAnimDerivedValue.value, {
		inputRange:[0,15],
		outputColorRange:["tomato","teal"]
	}) 

       // tried to use the one below, but app would crash without throwing any error
    
	// const backgroundColorInterpolation = useAnimatedStyle(() => {
       // 	const interpolation = interpolateColors(colorAnim.value, {
       //         inputRange:[0,15],
       //         outputColorRange:["tomato","teal"]
       //     }) 

	// 	return {
	// 	  backgroundColor: interpolation
	// 	};
	// });

	return (
			<Animated.View style={{   flex:1, backgroundColor:backgroundColorInterpolation}}>
                             <Button 
                                     onPress={() => {
                                         colorAnim.value = withTiming(15, {
                                             duration: 1000,
                                     });
                                    }
                              }/>
		      </Animated.View>
	);
};

export default testScreen

Package versions

  • React: 16.13.1
  • React Native:0.63.0
  • React Native Reanimated:^2.0.0-alpha.5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
terrysahaidakcommented, Sep 22, 2020

Hi there, in order to pass some animated values to the styles, you need to use useAnimatedStyle hook. Passing values directly is no longer supported with Reanimated v2 API (You can still use Reanimated v1 with new Value and all the cond, set, add etc though).

This particular example @atcode-soft provided is not working because the provided interpolateColor API is only compatible with Reanimated v1.

Unfortunately, there is no API for that in the core, but you can use the one react-native-redash provides.

import Animated, {
  useSharedValue,
  useAnimatedScrollHandler,
  useAnimatedStyle
} from "react-native-reanimated";
import { interpolateColors } from "react-native-radash";

const ComponentName = () => {
  const scrollOffset = useSharedValue(0);

  const scrollHandler = useAnimatedScrollHandler({
    onScroll: event => {
      scrollOffset.value = event.contentOffset.x;
    }
  });

  const backgroundStyles = useAnimatedStyle(() => {
    // make sure to run any interpolations inside the worklet
    // callback of useAnimatedStyle is a worklet
    // but also it knows you has access the scrollOffset value
    // so now it will re-run on each scroll change change
	
    // usage https://wcandillon.gitbook.io/redash/colors#interpolatecolor
    const backgroundColor = interpolateColors(
      scrollOffset.value,
      [0, width, width * 2, width * 3],
      ["#BFEAF5", "#BEECC4", "#FFE4D9", "#FFDDDD"]
    );
  }, []);

  return (
    <View style={styles.container}>
      <Animated.View style={[styles.slider, backgroundStyles]}>
        <Animated.ScrollView
          horizontal
          snapToInterval={width}
          decelerationRate="fast"
          showsHorizontalScrollIndicator={false}
          bounces={false}
          onScroll={scrollHandler}
          scrollEventThrottle={1}
        >
          ...
        </Animated.ScrollView>
      </Animated.View>
    </View>
  );
};
1reaction
danielanthonylcommented, Mar 12, 2022
import Animated, {
  useSharedValue,
  useAnimatedScrollHandler,
  useAnimatedStyle
} from "react-native-reanimated";
import { interpolateColors } from "react-native-radash";

const ComponentName = () => {
  const scrollOffset = useSharedValue(0);

  const scrollHandler = useAnimatedScrollHandler({
    onScroll: event => {
      scrollOffset.value = event.contentOffset.x;
    }
  });

  const backgroundStyles = useAnimatedStyle(() => {
    // make sure to run any interpolations inside the worklet
    // callback of useAnimatedStyle is a worklet
    // but also it knows you has access the scrollOffset value
    // so now it will re-run on each scroll change change
	
    // usage https://wcandillon.gitbook.io/redash/colors#interpolatecolor
    const backgroundColor = interpolateColors(
      scrollOffset.value,
      [0, width, width * 2, width * 3],
      ["#BFEAF5", "#BEECC4", "#FFE4D9", "#FFDDDD"]
    );
  }, []);

  return (
    <View style={styles.container}>
      <Animated.View style={[styles.slider, backgroundStyles]}>
        <Animated.ScrollView
          horizontal
          snapToInterval={width}
          decelerationRate="fast"
          showsHorizontalScrollIndicator={false}
          bounces={false}
          onScroll={scrollHandler}
          scrollEventThrottle={1}
        >
          ...
        </Animated.ScrollView>
      </Animated.View>
    </View>
  );
};

useAnimatedStyle has to return an object, found undefined instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interpolate Colors like a pro with React Native Reanimated 2
Today we are going to understand the concept behind the interpolateColor function from the react-native-reanimated (v2) package.
Read more >
Interpolate Colors like a pro with React Native Reanimated 2 ...
In this tutorial we'll learn the concept behind the interpolateColor function from React Native Reanimated. To improve the learning ...
Read more >
Reanimated 2 - the new approach to creating animations in ...
I encourage you to start using Reanimated 2 in your React Native projects ... The hook accepts an object where we can configure...
Read more >
Shared Values | React Native Reanimated - Software Mansion
Shared Values are among the fundamental concepts behind Reanimated 2.0. ... In order to create a Shared Value reference, you should use useSharedValue...
Read more >
How to Make Performant Animations in React Native using ...
Why should you use Reanimated to make smooth and quick animations and how to do it? ... 1. Benefits of React Native Reanimated...
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