React路由器V6模态重新加载背景页面位置(而不是保存滚动位置)

发布于 2025-02-10 12:30:52 字数 2212 浏览 1 评论 0原文

使用React Router Router V6。我已经设置了路由,以便将背景点传递给路由,而模态则以其他路由为背景页面正确打开。我在页面上向下滚动时打开模态时遇到了意外的行为 - 页面“重新加载”,所以它回到了页面的顶部,然后打开模态。模态关闭时,背景页面正确显示(向下滚动)。看起来很紧张。相反,我希望模态能够打开/关闭,而不会更改背景页面的滚动位置。我跟随非常紧密。

这是相关代码:

import React from "react";
import { Route, Routes, useLocation } from "react-router-dom";

const Routes: React.FC = () => {
  const location = useLocation();
  const state = location.state as {
    backgroundLocation?: Location;
  };

  return (
      {/* Pass location to Routes so that Page remains background when ModalVersion is opened */}
      <Routes location={state?.backgroundLocation || location}>
        <Route path={'/'} element={<Home />} />
        <Route path={'/page'} element={<Page />} />
        <Route path={'/page/:more_info'} element={<PageVersion />} />
      </Routes>

      {/* Open Modal if state.backgroundLocation is defined */}
      {state?.backgroundLocation && (
        <Routes>
          <Route path={'/page/:more_info'} element={<ModalVersion />} />
        </Routes>
      )}
  )

...

import { Link } form "@mui/material"    
import { Link as RouterLink } from "react-router-dom";

const Page: React.FC = () => {

 return (
   <APageComponentContainingALink>
    <Link
      component={RouterLink}
      to={"/page/some_info"}
      state={{ backgroundLocation: location }}
    >
      ...
    </Link>
   </ APageComponentContainingALink>
  )

}
export { Page };

...

  // Returns the same as PageVersion, but wrapped in a modal
  const ModalVersion: React.FC = () => {
    
      return (
        <Modal>
          <Info />
        </Modal>
      )
    
    }
    export { ModalVersion };

...

// Returns the same as ModalVersion, but on its own page
const PageVersion: React.FC = () => {

  return (
    <Info />
  )

}
export { PageVersion };

Working with React Router v6. I have routes set up so that backgroundLocation is passed to the Routes and a modal opens correctly with a different route as the background page. I came across unexpected behavior when opening a modal while scrolled down on the page though - the page 'reloads' so it's back at the top of the page and then opens the modal. When the modal closes, the background page shows correctly (scrolled down). It looks jittery. Instead, I want the modal to open/close without changing the scroll position of the background page. I followed the RRDv6 modal example quite closely.

Here's the relevant code:

import React from "react";
import { Route, Routes, useLocation } from "react-router-dom";

const Routes: React.FC = () => {
  const location = useLocation();
  const state = location.state as {
    backgroundLocation?: Location;
  };

  return (
      {/* Pass location to Routes so that Page remains background when ModalVersion is opened */}
      <Routes location={state?.backgroundLocation || location}>
        <Route path={'/'} element={<Home />} />
        <Route path={'/page'} element={<Page />} />
        <Route path={'/page/:more_info'} element={<PageVersion />} />
      </Routes>

      {/* Open Modal if state.backgroundLocation is defined */}
      {state?.backgroundLocation && (
        <Routes>
          <Route path={'/page/:more_info'} element={<ModalVersion />} />
        </Routes>
      )}
  )

...

import { Link } form "@mui/material"    
import { Link as RouterLink } from "react-router-dom";

const Page: React.FC = () => {

 return (
   <APageComponentContainingALink>
    <Link
      component={RouterLink}
      to={"/page/some_info"}
      state={{ backgroundLocation: location }}
    >
      ...
    </Link>
   </ APageComponentContainingALink>
  )

}
export { Page };

...

  // Returns the same as PageVersion, but wrapped in a modal
  const ModalVersion: React.FC = () => {
    
      return (
        <Modal>
          <Info />
        </Modal>
      )
    
    }
    export { ModalVersion };

...

// Returns the same as ModalVersion, but on its own page
const PageVersion: React.FC = () => {

  return (
    <Info />
  )

}
export { PageVersion };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2025-02-17 12:30:53

React路由器卷轴修复文档可能是您想要的:
https://v5.reactrouter.com/web/guides/guides/guides/scroll-Restoration

没有看到结果,我的假设是,由于组件在单页上渲染/卸载,因此DOM可能会发生一些高度调整。执行这些卷轴修复效果可能会阻止这种情况发生。

The React Router Scroll Restoration docs might be what you're looking for:
https://v5.reactrouter.com/web/guides/scroll-restoration

Without seeing the results, my assumption is that because the components are rendering/unmounting on a single page, there might be some height adjustment happening in the DOM. Doing these scroll restoration effects will likely stop that from happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文