網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
在移動(dòng)端中,用戶訪問(wèn)了一個(gè)列表頁(yè),上拉瀏覽列表頁(yè)的過(guò)程中,隨著滾動(dòng)高度逐漸增加,數(shù)據(jù)也將采用觸底分頁(yè)加載的形式逐步增加,列表頁(yè)瀏覽到某個(gè)位置,用戶看到了感興趣的項(xiàng)目,點(diǎn)擊查看其詳情,進(jìn)入詳情頁(yè),從詳情頁(yè)退回列表頁(yè)時(shí),需要停留在離開列表頁(yè)時(shí)的瀏覽位置上,react中沒有現(xiàn)成得保留組件狀態(tài)得方法。
但是有第三方庫(kù) react-activation 個(gè)人感覺這個(gè)好用!
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、安裝第三方庫(kù)
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、在需要保留狀態(tài)得組件上使用 KeepAlive 包裹
我要保留cate組件得狀態(tài)所以使用keepAlive包裹c(diǎn)ate組件
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
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_61102579/article/details/128836868
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-04-17 idea中將項(xiàng)目打成jar包并在其他項(xiàng)目中引用
- 2022-08-27 .Net實(shí)現(xiàn)延遲隊(duì)列_實(shí)用技巧
- 2022-04-08 Android如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)詳解_Android
- 2023-05-23 Golang拾遺之指針和接口的使用詳解_Golang
- 2024-02-16 SpringBoot 全局異常處理
- 2023-07-10 匿名內(nèi)部類、Lambda表達(dá)式、方法引用對(duì)比分析
- 2022-09-03 解決Python報(bào)錯(cuò)問(wèn)題[SSL:?SSLV3_ALERT_HANDSHAKE_FAILURE]_p
- 2022-06-30 Python使用Pillow添加水印_python
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支