Created
February 22, 2018 10:59
-
-
Save pistonsky/c72af9444400b50fa074d2f0aba6d6eb to your computer and use it in GitHub Desktop.
Sample smart error component which slides from top, shows for a second, and slides back up
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
import React, { Component } from 'react'; | |
import { connect } from 'react-redux'; | |
import { StatusBar, Text, Animated } from 'react-native'; | |
import { getErrorMessage, getErrorId } from '../reducers/selectors'; | |
const HIDDEN = -20; | |
const SHOWN = 0; | |
class ErrorNotification extends Component { | |
constructor(props) { | |
super(props); | |
this.animated = { | |
top: new Animated.Value(HIDDEN) | |
}; | |
this.state = { | |
shown: false | |
}; | |
} | |
componentWillReceiveProps(nextProps) { | |
if (nextProps.error_id !== this.props.error_id) { | |
this.setState({ shown: true }); | |
Animated.sequence([ | |
Animated.timing(this.animated.top, { | |
toValue: SHOWN, | |
useNativeDriver: true | |
}), | |
Animated.delay(1000), | |
Animated.timing(this.animated.top, { | |
toValue: HIDDEN, | |
useNativeDriver: true | |
}) | |
]).start(() => { | |
this.setState({ shown: false }); | |
}); | |
} | |
} | |
render() { | |
if (this.state.shown) { | |
return ( | |
<Animated.View | |
style={{ | |
position: 'absolute', | |
top: 0, | |
left: 0, | |
right: 0, | |
height: 20, | |
transform: [{ translateY: this.animated.top }], | |
overflow: 'hidden', | |
justifyContent: 'center', | |
alignItems: 'center', | |
backgroundColor: 'red' | |
}} | |
> | |
<Text style={{ fontSize: 8, color: 'white', fontWeight: 'bold' }}> | |
{this.props.message} | |
</Text> | |
</Animated.View> | |
); | |
} else { | |
return null; | |
} | |
} | |
} | |
function mapStateToProps(state) { | |
return { | |
message: getErrorMessage(state), | |
error_id: getErrorId(state) | |
}; | |
} | |
export default connect(mapStateToProps)(ErrorNotification); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment