reactjs 如何正确使用React路由器?[关闭]

nhaq1z21  于 2023-04-05  发布在  React
关注(0)|答案(2)|浏览(117)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2天前关闭。
Improve this question
我正在开发一个react网站。如果我在我的主页上向下滚动,然后点击导航栏的任何链接,那么新的组件渲染也会滚动,而不是从滚动0渲染。如何解决这个问题?
我期待着这个问题的解决方案

lf3rwulv

lf3rwulv1#

在App.js中尝试以下操作

const { pathname } = useLocation();
 useEffect(() => {
    document.documentElement.scrollTop = 0;
    document.scrollingElement.scrollTop = 0;
  }, [pathname]);
5hcedyr0

5hcedyr02#

尝试以下代码从滚动0呈现组件:

import { useEffect } from "react";

useEffect(() => {
  window.scrollTo(0, 0)
}, [])

相关问题