Skip to content

Instantly share code, notes, and snippets.

@hql287
Last active October 17, 2017 04:44
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 hql287/8ad4663feb2f516a6a86a306c8947cb0 to your computer and use it in GitHub Desktop.
Save hql287/8ad4663feb2f516a6a86a306c8947cb0 to your computer and use it in GitHub Desktop.
Self Closing Notification
class Notification extends Component {
constructor(props) {
super(props);
this.removeNoti = this.removeNoti.bind(this);
}
componentDidMount() {
this.timeout = setTimeout(this.removeNoti, 4000);
}
shouldComponentUpdate(nextProps) {
return this.props.notification !== nextProps.notification;
}
componentWillUnmount() {
clearTimeout(this.timeout);
}
removeNoti() {
const {removeNoti, notification} = this.props;
removeNoti(notification.id);
}
render() {
const {notification} = this.props;
return (
<Noti type={notification.type} onClick={this.removeNoti}>
<Message>
{notification.message}
</Message>
<Button onClick={this.removeNoti}>
Close
</Button>
</Noti>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment