最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Scrolling to top when the route changes in React router dom - Stack Overflow

programmeradmin0浏览0评论

I am using [email protected] and I am facing a problem. The routing is working fine but when I am in the middle of the page and click on any link in my navbar or any other link to route to another page the next page opens from the center and not from the top. After some digging, I discovered that there is a ScrollRestoration ponent from react-router-dom that can be used to scroll to the top instead of the traditional useEffect scroll to top solution. But in the example that they have given, I didn't understand how to use it inside createBrowserRouter.

Here is the code from my app.js file:

import { RouterProvider, createBrowserRouter } from 'react-router-dom'
import {
  Courses,
  Error,
  Home,
  HomeLayout,
  Login,
  Register,
  Testing,
  Course,
  Contact,
  About,
  FAQ,
  Events,
  EventDetails,
  Library,
  Admin,
} from '@/pages'

const router = createBrowserRouter([
  {
    path: '/',
    element: <HomeLayout />,
    errorElement: <Error />,
    children: [
      {
        index: true,
        element: <Home />,
      },
      {
        path: 'courses',
        element: <Courses />,
      },
      {
        path: 'single-course',
        element: <Course />,
      },
      {
        path: 'testing',
        element: <Testing />,
      },
      {
        path: 'contact',
        element: <Contact />,
      },
      {
        path: 'about-us',
        element: <About />,
      },
      {
        path: 'faq',
        element: <FAQ />,
      },
      {
        path: 'events',
        element: <Events />,
      },
      {
        path: 'event-details',
        element: <EventDetails />,
      },
      {
        path: 'library',
        element: <Library />,
      },
    ],
  },
  {
    path: '/login',
    element: <Login />,
  },
  {
    path: '/register',
    element: <Register />,
  },
  {
    path: '/admin',
    element: <Admin />,
  },
])

const App = () => {
  return <RouterProvider router={router} />
}

export default App

I am using [email protected] and I am facing a problem. The routing is working fine but when I am in the middle of the page and click on any link in my navbar or any other link to route to another page the next page opens from the center and not from the top. After some digging, I discovered that there is a ScrollRestoration ponent from react-router-dom that can be used to scroll to the top instead of the traditional useEffect scroll to top solution. But in the example that they have given, I didn't understand how to use it inside createBrowserRouter.

Here is the code from my app.js file:

import { RouterProvider, createBrowserRouter } from 'react-router-dom'
import {
  Courses,
  Error,
  Home,
  HomeLayout,
  Login,
  Register,
  Testing,
  Course,
  Contact,
  About,
  FAQ,
  Events,
  EventDetails,
  Library,
  Admin,
} from '@/pages'

const router = createBrowserRouter([
  {
    path: '/',
    element: <HomeLayout />,
    errorElement: <Error />,
    children: [
      {
        index: true,
        element: <Home />,
      },
      {
        path: 'courses',
        element: <Courses />,
      },
      {
        path: 'single-course',
        element: <Course />,
      },
      {
        path: 'testing',
        element: <Testing />,
      },
      {
        path: 'contact',
        element: <Contact />,
      },
      {
        path: 'about-us',
        element: <About />,
      },
      {
        path: 'faq',
        element: <FAQ />,
      },
      {
        path: 'events',
        element: <Events />,
      },
      {
        path: 'event-details',
        element: <EventDetails />,
      },
      {
        path: 'library',
        element: <Library />,
      },
    ],
  },
  {
    path: '/login',
    element: <Login />,
  },
  {
    path: '/register',
    element: <Register />,
  },
  {
    path: '/admin',
    element: <Admin />,
  },
])

const App = () => {
  return <RouterProvider router={router} />
}

export default App
Share Improve this question edited Sep 28, 2023 at 15:23 Drew Reese 204k18 gold badges240 silver badges271 bronze badges asked Sep 28, 2023 at 10:06 saimarshaddsaimarshadd 1673 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

The ScrollRestoration ponent also only works with the data routers, so you have to use one of the routers created using createBrowserRouter, createMemoryRouter, etc.

From the 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 remended you render it in the root route of your app

Create a layout route ponent to render as the root route element that renders ScrollRestoration and an Outlet for the nested routes.

Example:

import { RouterProvider, createBrowserRouter, Outlet } from 'react-router-dom'
...

const AppLayout = () => (
  <>
    <ScrollRestoration />
    <Outlet />
  </>
);

const router = createBrowserRouter([
  {
    element: <AppLayout />,
    children: [
      {
        path: '/',
        element: <HomeLayout />,
        errorElement: <Error />,
        children: [
          { index: true, element: <Home /> },
          { path: 'courses', element: <Courses /> },
          { path: 'single-course', element: <Course /> },
          { path: 'testing', element: <Testing /> },
          { path: 'contact', element: <Contact /> },
          { path: 'about-us', element: <About /> },
          { path: 'faq', element: <FAQ /> },
          { path: 'events', element: <Events /> },
          { path: 'event-details', element: <EventDetails /> },
          { path: 'library', element: <Library /> },
        ],
      },
      { path: '/login', element: <Login /> },
      { path: '/register', element: <Register /> },
      { path: '/admin', element: <Admin /> },
    ]
  },
]);

const App = () => {
  return <RouterProvider router={router} />
};

export default App;
发布评论

评论列表(0)

  1. 暂无评论