admin管理员组文章数量:1355648
I'm using react-router-dom
for implementing routing of my reactjs App!
but I have an issue with this , in which I can't find any working solution for scroll to top on page changing. can any body help me???
here is my routing piece of code:
<BrowserRouter basename={basename}>
<Switch>
<MobilePage hasNav navType="primary" exact path={`/restaurant`} ponent={VendorList} />
<MobilePage hasNav navType="primary" path={`/restaurant/:vendorCode`} ponent={VendorDetails} />
</Switch>
</BrowserRouter>
I'm using react-router-dom
for implementing routing of my reactjs App!
but I have an issue with this , in which I can't find any working solution for scroll to top on page changing. can any body help me???
here is my routing piece of code:
<BrowserRouter basename={basename}>
<Switch>
<MobilePage hasNav navType="primary" exact path={`/restaurant`} ponent={VendorList} />
<MobilePage hasNav navType="primary" path={`/restaurant/:vendorCode`} ponent={VendorDetails} />
</Switch>
</BrowserRouter>
Share
Improve this question
asked Jun 24, 2018 at 10:43
MohammadJavad SeyyediMohammadJavad Seyyedi
6847 silver badges21 bronze badges
3 Answers
Reset to default 5I am currently using the react-router-dom v5 (most of these solutions that I found was geared to v4) and I was surprised to not find a solution for pages to load to the top in a transition.
What solved my problem was installing react-router-scroll-top and put the wrapper in the like this:
const App = () => (
<Router>
<ScrollToTop>
<App/>
</ScrollToTop>
</Router>
)
and everything worked as expected.
add to VendorList
and VendorDetails
in ponentDidMount()
function window.scrollTo(0,0)
Install the plugin below https://www.npmjs./package/react-router-scroll-top
import ScrollToTop from 'react-router-scroll-top'
const App = () => (
<Router>
<ScrollToTop>
<App/>
</ScrollToTop>
</Router>
)
<ScrollToTop/>
本文标签: javascriptscroll to top on react reactrouterdom page changeStack Overflow
版权声明:本文标题:javascript - scroll to top on react react-router-dom page change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743969469a2570495.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论