Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate text color change in React-Native

Tags:

react-native

This problem has been bugging me for a while now and I am stuck. I want to animate the color property of my text when a user clicks on a Touchable.

For this I am using an tag.

What am I missing to make this work?

This is my component so far:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Text, View, TouchableWithoutFeedback, LayoutAnimation, Animated } from 'react-native';

import { CardSection } from './common';
import * as actions from '../actions';

class ListItem extends Component {
  state = {
    colorAnim: new Animated.Text('#000')
  };

  componentWillUpdate() {
    LayoutAnimation.easeInEaseOut();

    Animated.timing(
      this.state.colorAnim,
      {
        toValue: '#47bed1',
        duration: 5000,
      }
    ).start();
  }

  renderDescription () {
    const { library, expanded } = this.props;

    if (expanded) {
      return (
        <CardSection>
          <Text style={styles.textStyle}>
            { library.description }
          </Text>
        </CardSection>
      );
    }
  }

  render() {
    const { titleStyle } = styles;
    const { id, title } = this.props.library;

    return (
      <TouchableWithoutFeedback
        onPress={() => this.props.selectLibrary(id)}
      >
        <View>
          <CardSection>
            <Animated.Text
              style={{
                ...titleStyle,
                color: this.state.colorAnim
              }}
            >
              { title }
            </Animated.Text>
          </CardSection>
          { this.renderDescription() }
        </View>
      </TouchableWithoutFeedback>
    );
  }
}

const styles = {
  titleStyle: {
    fontSize: 18,
    paddingLeft: 15
  },

  textStyle: {
    paddingLeft: 18,
    paddingRight: 5
  }
}

const mapStateToProps = (state, ownProps) => {
  const expanded = state.selectedLibraryId === ownProps.library.id;

  return {
    expanded: expanded
  };
};

export default connect(mapStateToProps, actions)(ListItem);
like image 590
Walter Monecke Avatar asked Jul 12 '17 20:07

Walter Monecke


People also ask

How do you animate text color in React Native?

Put two text components (showing the same text) above each other (using absolute layout). You can then useNativeDriver and change the opacity of each text component. 2. Use react native reanimated v2 to change the color and archive native performance.

How do you animate a background color in React Native?

For animating between colors we need to interpolate between two values of colors. Now, as the state 'animation' is of type Animated. Value, there is a method called interpolate(), which takes a configuration object with keys.

How animations work in React Native?

Animations allow you to convey physically believable motion in your interface. React Native provides two complementary animation systems: Animated for granular and interactive control of specific values, and LayoutAnimation for animated global layout transactions.


1 Answers

Here is the thread for backgroundColor prop animation, which is almost the same: Animating backgroundColor in React Native

Basically, you can not use the color hex string directly, but declare your colorAnim as new Animated.Value(1). The value inside () should be integer such as 1.

And in the begining of render, the integer value is "interpolated" to real color like this:

var color = this.state.colorAnim.interpolate({
    inputRange: [0, 1],
    outputRange: ['#858a91', '#ffffff']
});

Then this color object can be used for the color prop of your Animated.Text

...
color: color,
...
like image 68
Alt Eisen Avatar answered Dec 16 '22 04:12

Alt Eisen