【发布时间】:2022-01-21 14:14:27
【问题描述】:
滚动恢复会向上滚动页面的 8/10,但不是一直滚动。不确定我的 ScrollToTop 组件是否有缺陷,或者我是否在 App 组件中错误地使用了路由和切换标签。下面分别是 ScrollToTop 和 App 组件。
import { useEffect } from "react";
import { useLocation } from "react-router-dom";
function ScrollToTop() {
const { pathname } = useLocation();
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
return null;
}
export default ScrollToTop;
return (
<div className="App">
<Switch>
<Route path='/explore'>
<ScrollToTop />
<Layout className='' currentUser={currentUser} posts={posts} handleLogout={handleLogout}>
<Explore posts={posts} />
</Layout >
</Route>
<Route path='/meta'>
<ScrollToTop />
<Layout currentUser={currentUser} posts={posts} handleLogout={handleLogout}>
<Meta currentUser={currentUser} posts={posts} />
</Layout>
</Route>
<Route path='/mana'>
<ScrollToTop>
<Layout currentUser={currentUser} posts={posts} handleLogout={handleLogout}>
<Mana currentUser={currentUser} posts={posts} />
</Layout>
</ScrollToTop>
</Route>
<Route path='/crypto'>
<ScrollToTop />
<Layout currentUser={currentUser} posts={posts} handleLogout={handleLogout}>
<Crypto currentUser={currentUser} posts={posts} />
</Layout>
</Route>
<Route path='/film'>
<ScrollToTop />
<Layout currentUser={currentUser} posts={posts} handleLogout={handleLogout}>
<Film currentUser={currentUser} posts={posts} />
</Layout>
</Route>
</Switch>
</div >
);
}
export default App;
【问题讨论】:
标签: reactjs react-native react-router-v4 scrolltop