Intro..
Sometimes, when working with long pages, it can be helpful to allow users to quickly scroll back to the top of the page. In React.js, this can be easily achieved with a few lines of code.
There are several ways to solve this problem.
Scroll To Top
There are two ways to move the scroll up when moving the page.
- Without Library
- Using Library
π‘ The library referred to here is react-router-dom.
Without Library: Custom Component
Enable to reschedule the scroll without a library, and the method is very simple.
Use component!
This method was used until related functions were introduced in the library. If not using the React-router-dom library, we can use it.
The component name is free, but it seems to be mostly used as ScrollToTop().
1// scrollToTop.tsx 2import { useEffect } from "react"; 3import { useLocation } from "react-router-dom"; 4 5export default function ScrollToTop() { 6 const { pathname } = useLocation(); 7 8 useEffect(() => { 9 window.scrollTo(0, 0); 10 }, [pathname]); 11 12 return null; 13}
Using Library Component: React-Router-Dom
React-router-dom covered in this section is only applicable to v6 (not sure if previous versions also had this feature).
The second method is to use <ScrollRestoration /> provided by a library that can be used without creating a separate custom component.
1// Root.tsx 2import { ScrollRestoration } from "react-router-dom"; 3 4export default function Root() { 5 return ( 6 <div> 7 <ScrollRestoration /> 8 </div> 9 ); 10}
If the above first or second one was applied correctly, it would work normally as shown in the gif below.
Where To Place It
It doesn't mean we use it anywhere, we have to use it in a specific location to work properly. If we use it incorrectly, the screen just comes out black...
Most of the time, on the Internet, it's used on top of the App component in index.tsx, as shown in the picture below.
However, in my case, if I used it in Index.tsx, I only got a black screen. Also, App.js didn't work as well.
Maybe App.js isn't just a component, it's a hub to route somewhere else.
(Of course, this analysis is not 100% accurate. This is why I thought to myself based on my previous experience. I asked stackoverflow to find out the underlying cause. β)
I don't know how <ScrollRestoration /> is structured, but based on my custom component, <ScrollToTop />, when the screen is first rendered through useEffect, it takes the x and y axes of the screen and sets it to 0,0
I think it's right to use it on top of the components that the screen is finally rendered,
so instead of index.tsx (taking App.js and simply putting it in the class "root" of index.html) or App.js (which is not actually rendering, but acting as a hub according to url), we have to put it on the outlet of Root.tsx that is finally rendering the screen.
Question And Answer
Q: Is there a standard for the location or file that uses it(ScrollToTop)? If so, what are the criteria? A: Straight from the ScrollRestoration docs:
β οΈ IMPORTANT
This feature only works if using a data router, see Picking a Router
You should only render one of these and it's recommended you render it in the root route of your app:
1import { ScrollRestoration } from "react-router-dom"; 2 3function RootRouteComponent() { 4 return ( 5 <div> 6 {/* ... */} 7 <ScrollRestoration /> 8 </div> 9 ); 10}
The ScrollRestoration didn't work in your index.tsx or App component because you rendered it outside the data router, i.e. outside RouterProvider.
It worked when you moved it into your root Root layout route component inside the RouterProvider.
URL:
https://medium.com/@glasshost/scroll-to-the-top-of-the-page-in-react-js-85a891158736
URL:
https://reactrouter.com/en/main/components/scroll-restoration


