Created
March 26, 2018 07:31
-
-
Save hpstuff/4e21abd6e62c03e364e19c5fb0040281 to your computer and use it in GitHub Desktop.
React Components
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
class TextChanges extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
animation: new Animated.Value(0), | |
currentText: '', | |
oldText: '' | |
}; | |
} | |
startAnimation() { | |
Animated.timing(this.state.animation, { | |
toValue: 1, | |
duration: 200, | |
easing: Easing.inOut(Easing.quad) | |
}).start(); | |
} | |
componentWillReceiveProps({text}) { | |
if (text === this.state.currentText) { | |
return; | |
} | |
this.updateState(text); | |
} | |
updateState(text) { | |
this.setState({ | |
currentText: text, | |
oldText: this.state.currentText | |
}); | |
this.state.animation.setValue(0); | |
} | |
componentDidUpdate() { | |
this.startAnimation(); | |
} | |
componentDidMount() { | |
this.updateState(this.props.text); | |
} | |
render() { | |
const { currentText, oldText } = this.state; | |
const oldStyle = { | |
opacity: this.state.animation.interpolate({ | |
inputRange: [0, 1], | |
outputRange: [1, 0] | |
}), | |
}; | |
const currentStyle = { | |
opacity: this.state.animation, | |
}; | |
return ( | |
<Animated.div style={containerStyle}> | |
<Animated.span style={oldStyle}>{currentText}</Animated.span> | |
{ oldText && <Animated.span style={currentStyle}>{oldText}</Animated.span> } | |
</Animated.div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment