每當(dāng)路線發(fā)生變化時,我都會使用 Scroll 組件滾動到頂部:Scroll.jsimport { useEffect } from 'react';import { withRouter } from 'react-router-dom';function ScrollToTop({ history }) { useEffect(() => { const unlisten = history.listen(() => { window.scrollTo(0, 0); }); return () => { unlisten(); } }, []); return (null);}export default withRouter(ScrollToTop);<Router> <Fragment> <ScrollToTop /> <Switch> <Route path="/" exact component={Home} /> </Switch> </Fragment></Router>我想要實(shí)現(xiàn)的是,每當(dāng)用戶在瀏覽器上單擊返回時,滾動到頂部應(yīng)該不起作用。每當(dāng)用戶在瀏覽器中單擊返回時,如何將滾動到頂部設(shè)置為 false?
僅當(dāng)用戶單擊返回時,如何將滾動設(shè)置為 false?
拉丁的傳說
2023-03-03 10:39:32