I am trying to scroll to the top on a component when its being rendered. My code looks like this:
const Terms = () => {
useEffect(() => {
window.scrollTo(0, 0);
console.log("effect rendered");
}, []);
return <>...</>
when I switch between 2 components it does not scroll to the top of the component. Even though console.log
renders each time as expected but not the scroll.
The solution that worked for my problem is:
useEffect(() => {
const element = document.getElementById('someId');
element.scrollIntoView();
}, []);
return (<div id="someId"> ... </div>)
Thanks for everyone still for the ideas!