javascript - Material UI Menu Using Routes 在导航更改时不会滚动到顶部
问题描述
在使用 Material UI 的 MenuItem Link 组件进行导航时,我遇到了滚动问题。
当我导航到不同的组件时,它会保持上次导航的滚动位置。但是,我希望它在导航更改时恢复并从顶部开始。
import React from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import { MenuItem } from '@material-ui/core';
import { Link } from 'react-router-dom'
function TopMenu() {
return (
<AppBar>
<Toolbar>
<MenuItem component={Link} to='/'>
<Typography >
It doesn't scroll to the top when I navigate the the main page.
</Typography>
</MenuItem>
<Toolbar>
<AppBar>
);
}
export default TopMenu;
以下解决方案均不适用于我的情况:
componentDidMount() {
window.scrollTo(0, 0)
}
useEffect(() => {
window.scrollTo(0, 0)
}, [])
在此先感谢您的帮助!
解决方案
推荐阅读
- python - 不知何故我无法安装 Homebrew
- php - WooCommerce Ajax 添加到购物车数量不起作用
- webpack - Webpack 5:防止重复:入口依赖和 SplitChunksPlugin
- networking - 如果我们已经有外部负载均衡器,为什么还要使用内部负载均衡器?
- java - 更改字体后复选框不显示
- callback - Thrift 或 gRPC 中服务的动态绑定/将服务作为参数传递
- python - 如何在 Amazon Linux 2 AMI 上安装 pythonocc-core?
- python - 我正在尝试围绕python中的给定索引旋转数组
- unity3d - 如何仅在加速时限制玩家的速度?
- python - Kosaraju 算法 - 计算 SCC