Scrolling to the page-top is achievable in two ways.
Let's look at both methods on how to implement scroll to top.
The
We can use the
useEffect(() => {
window.scrollTo(0,0)
},[])
The example code shows links to other pages at the bottom of the page. Without
import { useEffect } from "react"
import { Link } from "react-router-dom"
const Post = () => {
useEffect(() => {
window.scrollTo(0,0)
},[])
return(
<>
<div>
<h1>Page Title</h1>
<p>Some long page content</p>
</div>
<div>
<span><Link to="/post1">Another Post</Link></span>
<span><Link to="/post2">Another Long Post</Link></span>
</div>
</>
)
}
export default Post
Placing the scroll code inside the
import { useEffect } from "react"
export const useScrollTo = (x: number, y: number) => {
useEffect(() => {
window.scrollTo(x,y)
})
}
The usage is simple as placing
import { useScrollTo } from "./components/useScrollTo";
import { Link } from "react-router-dom"
const Post = () => {
useScrollTo(0,0)
return(
<>
<div>
<h1>Page Title</h1>
<p>Some long page content</p>
</div>
<div>
<span><Link to="/post1">Another Post</Link></span>
<span><Link to="/post2">Another Long Post</Link></span>
</div>
</>
)
}
export default Post
Scrolling to the top using useRef requires a ref element located at the top of the page.
Below is a code example.
import { useEffect, useRef } from "react"
import { Link } from "react-router-dom";
const Home = () =>{
const refToTop = useRef<HTMLInputElement>(null);
useEffect(() => {
refToTop.current && refToTop.current.scrollIntoView();
})
return(
<div ref={refToTop}>
<div>
<h1>Page Title</h1>
<p>Some long page content</p>
</div>
<div>
<span><Link to="/post1">Another Post</Link></span>
<span><Link to="/post2">Another Long Post</Link></span>
</div>
</div>
)
}
export default Home;
Like window.scrollTo(), we can delegate the code to a react hook.
import { useEffect, useRef } from "react"
export const useRefScrollTo = () => {
const scrollToTop = useRef<HTMLInputElement>(null);
useEffect(() => {
scrollToTop.current && scrollToTop.current.scrollIntoView();
})
return scrollToTop
}
That simplifies our component to a certain stage.
import { Link } from "react-router-dom";
import { useRefScrollTo } from "./useRefScrollTo";
const Home = () =>{
const refToTop = useRefScrollTo()
return(
<div ref={refToTop}>
<div>
<h1>Page Title</h1>
<p>Some long page content</p>
</div>
<div>
<span><Link to="/post1">Another Post</Link></span>
<span><Link to="/post2">Another Long Post</Link></span>
</div>
</div>
)
}
export default Home
We can further animate the scroll effects with useRef's scrollIntoView function.
import { useEffect, useRef } from "react"
export const useRefScrollTo = (scrollProps: ScrollIntoViewOptions) => {
const scrollToTop = useRef<HTMLInputElement>(null);
useEffect(() => {
scrollToTop.current && scrollToTop.current.scrollIntoView({...scrollProps});
})
return scrollToTop
}
With that, we can use
import { Link } from "react-router-dom";
import { useRefScrollTo } from "./useRefScrollTo";
const Home = () =>{
const refToTop = useRefScrollTo({behavior: 'smooth', block: 'start', inline: 'nearest'})
return(
<div ref={refToTop}>
<div>
<h1>Page Title</h1>
<p>Some long page content</p>
</div>
<div>
<span><Link to="/post1">Another Post</Link></span>
<span><Link to="/post2">Another Long Post</Link></span>
</div>
</div>
)
}
export default Home