react中切换导航栏时记录每个页面的页面滚动位置,当切换回之前的页面,页面滚动位置为上次浏览的位置

实现上述功能的步骤如下:

  1. 安装必要的依赖包,包括react、react-dom、react-router-dom 和 react-scroll。

  2. 设计页面结构,包含一个导航栏和多个页面内容。

  3. 使用 react-router-dom 来管理页面路由,使用函数式组件编写每个页面组件。

  4. 在导航栏中添加切换页面的链接,并使用 react-scroll 来处理点击链接时滚动到页面相应位置的功能。

  5. 使用 useEffect 钩子来记录每个页面滚动位置,将滚动位置存储到本地存储中。

  6. 在使用 react-router-dom 的路由组件中,使用 useHistory 钩子来监听路由切换事件,以便在切换页面时记录当前页面的滚动位置。

  7. 切换回之前的页面时,使用 useEffect 钩子和本地存储中保存的滚动位置来还原之前滚动到的位置。

以下是大致的代码实现:

import React, { useEffect, useState } from 'react';
import { BrowserRouter as Router, Switch, Route, useHistory, useLocation } from 'react-router-dom';
import { Link as ScrollLink } from 'react-scroll';
import './App.css';

function App() {
  const [scrollPositions, setScrollPositions] = useState({});

  useEffect(() => {
    const savedScrollPositions = JSON.parse(localStorage.getItem('scrollPositions') || '{}');
    setScrollPositions(savedScrollPositions);
  }, []);

  useEffect(() => {
    localStorage.setItem('scrollPositions', JSON.stringify(scrollPositions));
  }, [scrollPositions]);

  return (
    <Router>
      <Nav />
      <Switch>
        <Route path="/" exact>
          <Home />
        </Route>
        <Route path="/about">
          <About />
        </Route>
        <Route path="/contact">
          <Contact />
        </Route>
      </Switch>
    </Router>
  );

  function Nav() {
    const { pathname } = useLocation();

    return (
      <nav>
        <ScrollLink to="home" smooth>首页</ScrollLink>
        <ScrollLink to="about" smooth>关于</ScrollLink>
        <ScrollLink to="contact" smooth>联系我们</ScrollLink>
      </nav>
    );
  }

  function Home() {
    const { pathname } = useLocation();
    const history = useHistory();

    useEffect(() => {
      window.scrollTo(0, scrollPositions[pathname] || 0);
    }, [pathname]);

    useEffect(() => {
      return () => {
        setScrollPositions({ ...scrollPositions, [pathname]: window.pageYOffset });
      };
    }, [pathname]);

    return (
      <section id="home">
        <h1>欢迎来到我们的网站</h1>
      </section>
    );
  }

  function About() {
    const { pathname } = useLocation();
    const history = useHistory();

    useEffect(() => {
      window.scrollTo(0, scrollPositions[pathname] || 0);
    }, [pathname]);

    useEffect(() => {
      return () => {
        setScrollPositions({ ...scrollPositions, [pathname]: window.pageYOffset });
      };
    }, [pathname]);

    return (
      <section id="about">
        <h2>关于我们</h2>
        <p>我们是一个专业的网站开发团队,致力于为客户提供优质的网站建设服务。</p>
      </section>
    );
  }

  function Contact() {
    const { pathname } = useLocation();
    const history = useHistory();

    useEffect(() => {
      window.scrollTo(0, scrollPositions[pathname] || 0);
    }, [pathname]);

    useEffect(() => {
      return () => {
        setScrollPositions({ ...scrollPositions, [pathname]: window.pageYOffset });
      };
    }, [pathname]);

    return (
      <section id="contact">
        <h2>联系我们</h2>
        <p>如果您有任何问题或建议,请随时与我们联系。</p>
      </section>
    );
  }
}

export default App;

当用户在页面中滚动时,各个页面的滚动位置会记录在本地存储中。当用户切换页面时,应用会读取本地存储中的滚动位置,并滚动到上次滚动到的位置。如果用户还没有滚动页面,滚动位置默认为0。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React,可以使用一些技术来缓存之前页面的数据,以便在切换页面可以快速加载和显示数据。以下是一些常见的缓存数据的方法: 1. 状态管理库:使用状态管理库(如Redux、MobX)来管理应用的数据状态。通过将数据存储在全局状态,可以在切换路由保留数据。当再次返回到之前页面,可以直接从状态获取数据进行显示。 2. 组件内部缓存:在组件内部使用state或者实例变量来缓存数据。当切换路由,组件被卸载,但是这些数据仍然存在于组件的状态或实例。再次访问该页面,可以从组件的状态或实例获取数据进行展示。 3. 浏览器缓存:使用浏览器的缓存机制(如LocalStorage、SessionStorage、IndexedDB)将数据存储在本地。当切换路由,可以先从缓存尝试获取数据,如果存在则使用缓存数据进行展示。注意,使用浏览器缓存需要考虑数据的有效期和清理策略。 4. 路由级别缓存:一些路由库(如react-router)提供了缓存路由组件的功能。当切换之前页面,路由库会自动从缓存获取组件并显示。这样可以避免重新渲染和加载数据,提高页面的渲染性能。 需要根据具体的应用场景和需求选择合适的缓存方法。在实现缓存,还要考虑数据的更新和清理策略,以确保缓存的数据与实际数据保持同步。同,也要注意缓存数据的大小和性能影响,避免过多的数据存储和加载导致性能问题。 希望对你有所帮助!如有任何疑问,请随提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值