Skip to content

Commit

Permalink
Remove click handler on Router unmount.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Dec 10, 2018
1 parent 1ebf7f1 commit 8789553
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions packages/webiny-react-router/src/Router.cmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ class Router extends React.Component<*, *> {

unlisten = null;

clickHandler = (event: Event) => {
// $FlowFixMe
const a = event.path.find(el => el.tagName === "A");
if (!a) {
return;
}

if (a.href.endsWith("#") || a.target === "_blank") {
return;
}

// Check if it's an anchor link
if (a.href.indexOf("#") > -1) {
return;
}

if (a.href.startsWith(window.location.origin)) {
event.preventDefault();
const { router } = this.props;
const { history } = router;
let url = parse(a.href, true);
history.push(url.pathname, router.config.basename);
}
};

componentDidMount() {
const { router } = this.props;
const { history } = router;
Expand All @@ -19,29 +44,7 @@ class Router extends React.Component<*, *> {
});
});

document.addEventListener("click", function(event: Event) {
// $FlowFixMe
const a = event.path.find(el => el.tagName === "A");
if (!a) {
return;
}

if (a.href.endsWith("#") || a.target === "_blank") {
return;
}

// Check if it's an anchor link
if (a.href.indexOf("#") > -1) {
return;
}

if (a.href.startsWith(window.location.origin)) {
event.preventDefault();

let url = parse(a.href, true);
history.push(url.pathname, router.config.basename);
}
});
document.addEventListener("click", this.clickHandler);

router.matchRoute(history.location.pathname).then(route => {
this.setState({ route });
Expand All @@ -59,6 +62,8 @@ class Router extends React.Component<*, *> {
componentWillUnmount() {
this.unlisten && this.unlisten();
this.unlisten = null;

document.removeEventListener("click", this.clickHandler);
}

render() {
Expand Down

0 comments on commit 8789553

Please sign in to comment.