React Router Dom v6:在路由更改时滚动到顶部
我在ReactJ上制作了一个网站,并使用“ React-Router-Dom V6”链接了各个页面。每当我从一个页面过渡到另一页时,第二页总是在当前位置而不是顶部加载。我尝试了许多教程和解决方案,但是它们都有效,直到React-Router-dom的V5。
I made a website on Reactjs and I have linked various pages using 'react-router-dom v6'. Whenever I transition from one page to another, the second page always loads on the current location instead of the top. I have tried many tutorials and solutions but they all are valid till v5 of react-router-dom.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过创建一个包装函数并将其包装在所有路由周围解决了以下问题。
请按照以下步骤操作:
1:您需要导入以下内容:
2:在“App”函数上方编写一个包装函数:
3:现在将您的路由包装在包装函数中:
这应该可以解决问题。
I solved the following issue by creating a wrapper function and wrapping it around all the routes.
Follow the following steps:
1: You need to import the following:
2: Write a wrapper function just above the "App" function:
3: Now wrap your routes within the wrapper function:
This should solve the issue.