3
votes

I'm following the React Native tutorial from: https://facebook.github.io/react-native/docs/animated.html

However, I got the following warning when I ran my code: Failed prop type: Invalid prop opacity of type object supplied to RCTView

And the component just disappear without animation when fade() got called.

And here is a sample code:

import React, { Component } from 'react';
import {
  AppRegistry,
  Text,
  View,
  Animated,
  StyleSheet
} from 'react-native';

import Icon from 'react-native-vector-icon/FontAwesome'

export default class Sample extends Component {
  state = {
    fadeAnim: new Animated.Value(0),
  }
  fade() {
    Animated.timing(                  // Animate over time
      this.state.fadeAnim,            // The animated value to drive
      {
        toValue: 1,                   // Animate to opacity: 1 (opaque)
        duration: 10000,              // Make it take a while
      }
    ).start();                        // Starts the animation
  }
  render() {
    let { fadeAnim } = this.state;

    return (
      <View style={styles.container}>
        <TouchableHighlight onPress={() => {this.fade()}}>
          <Icon name="circle" size={30} color="#fff" style={{opacity: fadeAnim}}
          >
        </TouchableHighlight>
      </View>
    );
  }
......
}
3

3 Answers

-2
votes

There is a problem in your code:

Icon tag can not be used in animcation, so react native complaint.

Instead, you should wrap the Icon with either: TouchableHighlight, TouchableNativeFeedback, TouchableOpacity, TouchableWithoutFeedback whichever works.

https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html

And bind the property to the Touchable* rather than Icon.

8
votes

You should change the View to an Animated.View. Then optionally you can add an interpolated value of fadeAnim for more fine grained control.

Something like this should work...

render() {
    const { fadeAnim } = this.state;

    // optionally tweak the input / output range to suit your needs
    const opacity = fadeAnim.interpolate({
      inputRange: [0, 1],
      outputRange: [0, 1]
    });

    return (
      <Animated.View style={[styles.container, opacity]}>
        <TouchableHighlight onPress={() => {this.fade()}}>
          <Icon name="circle" size={30} color="#fff"
          >
        </TouchableHighlight>
      </Animated.View>
    );
  }

You may not want to fade the container view, in which case move the Animated.View inside the Touchable.

0
votes

Try the opacity using alpha-value of the background color instead.

<Icon name="circle" size={30} color="#fff" style={{opacity: fadeAnim}} />

to

...
let faceRgb = 'rgba(0,0,0,' + faceAnim + ' )';

return (
  ...
  <Icon name="circle" size={30} color="#fff" style={{backgroundColor: faceRgb}} />
  ...
)