Skip to content

Instantly share code, notes, and snippets.

@rob-mcgrail
Created September 6, 2017 02:27
Show Gist options
  • Save rob-mcgrail/38186216e64a1fd74a6657bbd156688b to your computer and use it in GitHub Desktop.
Save rob-mcgrail/38186216e64a1fd74a6657bbd156688b to your computer and use it in GitHub Desktop.
import { Component } from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router-native';
const HistoryListener = class extends Component {
static propTypes = {
history: PropTypes.object.isRequired,
callback: PropTypes.func.isRequired,
delay: PropTypes.number
}
static defaultProps = {
delay: 0
}
constructor(props) {
super(props);
const callback = () => {
setTimeout(() => {
props.callback();
}, props.delay);
};
this.state = {
unlisten: props.history.listen(callback)
};
}
componentWillUnmount() {
this.state.unlisten();
}
render() {
return null;
}
};
export default withRouter(HistoryListener);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment