網站首頁 編程語言 正文
前言
在移動端中,用戶訪問了一個列表頁,上拉瀏覽列表頁的過程中,隨著滾動高度逐漸增加,數據也將采用觸底分頁加載的形式逐步增加,列表頁瀏覽到某個位置,用戶看到了感興趣的項目,點擊查看其詳情,進入詳情頁,從詳情頁退回列表頁時,需要停留在離開列表頁時的瀏覽位置上,react中沒有現成得保留組件狀態得方法。
但是有第三方庫 react-activation 個人感覺這個好用!
提示:以下是本篇文章正文內容,下面案例可供參考
一、安裝第三方庫
npm i react-activation
二、配置操作
1、在根目錄引入 AliveScope
import {AliveScope} from 'react-activation'
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
? <BrowserRouter>
? ? <Provider store={store}>
? ? ? <AliveScope>
? ? ? ? <App />
? ? ? </AliveScope>
? ? </Provider>
? </BrowserRouter>
);
2、在需要保留狀態得組件上使用 KeepAlive 包裹
我要保留cate組件得狀態所以使用keepAlive包裹cate組件
import { Navigate } from 'react-router-dom'
import {KeepAlive} from 'react-activation'
// 懶加載路由需要放到普通路由最下面
import NotFound from '../pages/notFound'
import Layout from '../pages/Layout'
import Home from '../pages/Layout/Home'
import Cate from '../pages/Layout/Cate'
import CateItem from '../pages/Layout/CateItem'
import ShopCar from '../pages/Layout/ShopCar'
import Me from '../pages/Layout/Me'
import ItemAll from '../pages/ItemAll'
const routerList = [
? { path: '/', element: <Navigate to="/home" /> },
? {
? ? path: '/home', element:<Layout />,children:[
? ? ? {index:true, element: <Navigate to="index" />},
? ? ? {path:'index', element: <Home />},
? ? ? {path:'cate', element: <KeepAlive><Cate /></KeepAlive>}, ?//這里需要包裹
? ? ? {path:'cateItem', element: <CateItem />},
? ? ? {path:'shopcar', element: <ShopCar />},
? ? ? {path:'Me', element: <Me />},
? ? ]
? },
? { path: '*', element: <NotFound /> }
]
export default routerList
總結
原文鏈接:https://blog.csdn.net/weixin_61102579/article/details/128836868
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-05-27 Flutter狀態管理Bloc之登錄示例_Android
- 2022-01-31 關于el-form中的rules未生效問題的解決方法
- 2023-07-13 react中使用高階函數
- 2023-06-16 瞅一眼就能學會的GO并發編程使用教程_Golang
- 2022-09-26 Redis刪除策略的三種方法及逐出算法_Redis
- 2022-05-11 Python實現圖書借閱管理系統_python
- 2024-03-14 SpringBoot中RestTemplate 發送http請求
- 2022-07-20 Python對PDF文件的常用操作方法詳解_python
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支