Why isn't this working as expected? Sometimes it works, and sometimes it doesn't - i cannot figure this one out. The piece of code with ScrollIntoView was copied from another js file with another page, and in that one it works just fine?
The ID's refTP and refPP are found within div tags in ReferencesPP and ReferencesTP
import React, {useState} from 'react'
import ReferencesPP from './referencesPP'
import ReferencesTP from './referencesTP'
import "./references.css"
function ReferencesPage(){
const reftp = document.getElementById("refTP");
const refpp = document.getElementById("refPP");
const [page, setPage] = useState(false);
const handleClick = (id) => {
if(id === 0 && page===true){
reftp.scrollIntoView({ behavior: "smooth" });
setPage(false);
} else if(id === 1 && page===false){
refpp.scrollIntoView({ behavior: "smooth" });
setPage(true);
}
}
return(
<div className="references-main-container">
<ul>
<li id="anchor1" onClick={()=>handleClick(0)}></li>
<li id="anchor2"onClick={()=>handleClick(1)}></li>
</ul>
<ReferencesTP />
<ReferencesPP />
</div>
)
}
export default ReferencesPage