Skip to content

Instantly share code, notes, and snippets.

@norbajunior
Last active November 28, 2017 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norbajunior/aff9986248434cef20d9c638df790d44 to your computer and use it in GitHub Desktop.
Save norbajunior/aff9986248434cef20d9c638df790d44 to your computer and use it in GitHub Desktop.
import { Animated, Easing } from 'react-native'
class Notification extends Component {
constructor(props) {
super(props)
this.state = { offset: new Animated.Value(props.offset) } // agora recebemos o offset inicial calculado
} // do componente pai
componentDidMount() {
Animated.sequence([
Animated.delay(this.props.delay), // acrescentamos aqui um delay
Animated.spring(this.state.offset, {
tension: -5,
toValue: 0
}),
Animated.timing(this.state.offset, {
duration: 1000,
toValue: -100,
easing: Easing.easeInOut
})
]).start(() => this.props.removeNotification(this.props.id)) // na função start passamos um callback que será
} // chamado ao completar a sequência de animações
// para remover a notificação que já foi mostrada
// ...
}
class TopBarNotification extends Component {
render() {
return this.props.notifications.map((notification, index) => {
return (
<Notification
key={notification.id}
offset={-(100 * (index + 1))} // agora cada notificação tem um offset calculado
delay={index === 0 ? 100 : (index + 1) * 2000} // e um delay diferente também, pra que se apresente sucessivamente
removeNotification={id => this.props.removeNotification(id)} // após terminar a animação removemos a notificação
{...notification}
/>
)
})
}
}
class App extends Component {
state = { notifications: [] }
removeNotification(id) {
this.setState((state) => {
return {
notifications: state.notifications.filter(notification => notification.id !== id)
}
})
}
render () {
<View>
<TopBarNotification
{...this.state}
removeNotification={id => this.removeNotification(id)}
/>
<Button
onPress={() => this.setState({ notifications: newNotifications })}
title="Receive Notifications"
/>
</View>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment