Last active
April 25, 2017 10:56
-
-
Save williamtran29/98f07b0ec25292f8f4a138bd83ddd353 to your computer and use it in GitHub Desktop.
Create FadeIn Animation for any React Native Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @flow */ | |
import React, { Component, PropTypes } from 'react' | |
import { | |
Animated | |
} from 'react-native' | |
export default class FadeIn extends Component { | |
static propTypes = { | |
children: PropTypes.node, | |
inputRange: React.PropTypes.array, | |
style: PropTypes.any | |
} | |
constructor(props) { | |
super(props) | |
this._animatedValue = new Animated.Value(0) | |
} | |
componentDidMount() { | |
Animated.timing( // Uses easing functions | |
this._animatedValue, // The value to drive | |
{ | |
toValue: 1, // Target | |
duration: 2000 // Configuration | |
}, | |
).start() // Don't forget start! | |
} | |
render() { | |
const { children, inputRange = [0, 1], style } = this.props | |
const opacity = this._animatedValue.interpolate({ | |
inputRange, | |
outputRange: [0, 1], | |
extrapolate: 'clamp' | |
}) | |
return ( | |
<Animated.View style={{ opacity, ...style }}> | |
{children} | |
</Animated.View> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment