From 915d7e7b8a6380fbb2b227814f6528b4c32a6fa9 Mon Sep 17 00:00:00 2001 From: Benjamin Michel Date: Tue, 9 Jan 2018 16:08:23 +0100 Subject: [PATCH 1/3] Prevent error when clicking a link to an anchor that does not exist --- src/scrollchor.jsx | 7 +++++-- src/utils.js | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/scrollchor.jsx b/src/scrollchor.jsx index f0e1cb3..2705976 100644 --- a/src/scrollchor.jsx +++ b/src/scrollchor.jsx @@ -40,8 +40,11 @@ export default class Scrollchor extends React.Component { this._beforeAnimate(event); event && event.preventDefault(); const id = animateScroll(this._to, this._animate); - this._disableHistory || updateHistory(id); - this._afterAnimate(event); + + if (id) { + this._disableHistory || updateHistory(id); + this._afterAnimate(event); + } } componentWillReceiveProps (props) { diff --git a/src/utils.js b/src/utils.js index 99291b2..60ddeaa 100644 --- a/src/utils.js +++ b/src/utils.js @@ -5,6 +5,10 @@ export function animateScroll (id, animate) { const element = id ? document.getElementById(id) : document.body; warning(element, `Cannot find element: #${id}`); + if (!element) { + return null + } + const { offset, duration, easing } = animate; const start = getScrollTop(); const to = getOffsetTop(element) + offset; @@ -39,6 +43,9 @@ function setScrollTop (position) { } function getOffsetTop (element) { + if (!element) { + return + } const { top } = element.getBoundingClientRect(); return top + getScrollTop(); } From 94439b4302c175f3ec3d634e335d1f99f6d72f45 Mon Sep 17 00:00:00 2001 From: Benjamin Michel Date: Tue, 9 Jan 2018 16:09:29 +0100 Subject: [PATCH 2/3] Removed useless test --- src/utils.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/utils.js b/src/utils.js index 60ddeaa..2d72f19 100644 --- a/src/utils.js +++ b/src/utils.js @@ -43,9 +43,6 @@ function setScrollTop (position) { } function getOffsetTop (element) { - if (!element) { - return - } const { top } = element.getBoundingClientRect(); return top + getScrollTop(); } From f83f2a6328158509efaf70392f0fac88020c8c22 Mon Sep 17 00:00:00 2001 From: Benjamin Michel Date: Tue, 9 Jan 2018 16:32:55 +0100 Subject: [PATCH 3/3] Fixed lint --- src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 2d72f19..94beef0 100644 --- a/src/utils.js +++ b/src/utils.js @@ -6,7 +6,7 @@ export function animateScroll (id, animate) { warning(element, `Cannot find element: #${id}`); if (!element) { - return null + return null; } const { offset, duration, easing } = animate;