添加一個onClick方法。onClick方法傳入一個錨點的id,然后用下面的函數來找到錨點並跳轉到錨點。
scrollToAnchor = (anchorName) => {
if (anchorName) {
// 找到錨點
let anchorElement = document.getElementById(anchorName);
// 如果對應id的錨點存在,就跳轉到錨點
if(anchorElement) { anchorElement.scrollIntoView(); }
}
}