Skip to content

Commit

Permalink
Merge pull request #23 from habx/no-throw
Browse files Browse the repository at this point in the history
Prevent error when clicking a link to an anchor that does not exist
  • Loading branch information
bySabi authored Jan 9, 2018
2 parents 4315226 + f83f2a6 commit 0b24262
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/scrollchor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0b24262

Please sign in to comment.