From dcf59de9d4366f47a2073247eafed9cef16a74ba Mon Sep 17 00:00:00 2001 From: Kyle Sosnowski Date: Mon, 13 May 2019 22:10:40 -0700 Subject: [PATCH] Prevent undefined exception on missing ID --- src/anchor-link.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/anchor-link.js b/src/anchor-link.js index 05a0c73..a6c6e99 100644 --- a/src/anchor-link.js +++ b/src/anchor-link.js @@ -20,11 +20,15 @@ class AnchorLink extends Component { } const id = e.currentTarget.getAttribute('href').slice(1) const $anchor = document.getElementById(id); - const offsetTop = $anchor.getBoundingClientRect().top + window.pageYOffset; - window.scroll({ - top: offsetTop - offset(), - behavior: 'smooth' - }) + + if ($anchor) { + const offsetTop = $anchor.getBoundingClientRect().top + window.pageYOffset; + window.scroll({ + top: offsetTop - offset(), + behavior: 'smooth' + }) + } + if (this.props.onClick) {this.props.onClick(e)} } render() {