I'm trying to create a simple action, that when the page loads, the page will scroll to a certain position.
But no matter what I do, the action does not fire.
Is there another way to do this in a React app?
for example:
window.scrollBy(0, 850);
I'm trying to create a simple action, that when the page loads, the page will scroll to a certain position.
But no matter what I do, the action does not fire.
Is there another way to do this in a React app?
for example:
window.scrollBy(0, 850);
wrap it inside useEffect
:
useEffect(() => {
window.scrollBy(0, 850);
}, []);
that way, it runs when the page mounts.
Yes you can do this through a ref
Steps
useEffect(() => {
ref.current.scrollIntoView()
} ,[])
The same thing applies if you want to just scroll to a certain height, you can just wrap it in a useEffect that fires when the component is mounted