react-router-dom?v6?通過outlet實(shí)現(xiàn)keepAlive?功能的實(shí)現(xiàn)
本文主要介紹了react-router-dom v6 通過outlet實(shí)現(xiàn)keepAlive 功能的實(shí)現(xiàn),具體如下:

keepAlive代碼:
import React, { useRef, useEffect, useReducer, useMemo, memo } from 'react'
import { TransitionGroup, CSSTransition } from 'react-transition-group'
import { useLocation } from 'react-router-dom'
const KeepAlive = props => {
const { include, keys, children } = props
const { pathname } = useLocation()
const componentList = useRef(new Map())
const forceUpdate = useReducer(bool => !bool)[1] // 強(qiáng)制渲染
const cacheKey = useMemo(() => pathname + "__" + keys[pathname], [pathname, keys]) // eslint-disable-line
const activeKey = useRef(null)
useEffect(() => {
componentList.current.forEach(function(value, key) {
const _key = key.split("__")[0]
if (!include.includes(_key) || (_key === pathname)) {
this.delete(key)
}
}, componentList.current)
activeKey.current = cacheKey
if (!componentList.current.has(activeKey.current)) {
componentList.current.set(activeKey.current, children)
}
forceUpdate()
}, [cacheKey, include]) // eslint-disable-line
return (
<TransitionGroup component={ null }>
{
Array.from(componentList.current).map(([key, component]) =>
<CSSTransition
key={ key }
appear={ true }
timeout={ 500 }
classNames='fade'>
{
key === activeKey.current ?
<div
className={
`layout-container${include.includes(key.split("__")[0]) ? " keep-alive-fade": ""}`
}>
{ component }
</div> :
<div
className='layout-container__keep-alive'
style={{ display: 'none' }}>
{ component }
</div>
}
</CSSTransition>
)
}
</TransitionGroup>
)
}
export default memo(KeepAlive)
main.js 中調(diào)用
import PropTypes from 'prop-types'
import { useLocation, useOutlet } from 'react-router-dom'
import { connect } from 'react-redux'
import { Layout } from 'antd'
import { TransitionGroup, CSSTransition } from 'react-transition-group'
import KeepAlive from '@/components/common/keepAlive'
import { isKeepAlive } from '@/service/config'
const Main = props => {
const { fullScreen, cacheRoutes, cacheKeys } = props
const outlet = useOutlet()
const { pathname } = useLocation()
return (
<Layout.Content className={{ "layout-main": true, "full-screen": fullScreen }}>
<section>
{
// isKeepAlive 生產(chǎn)環(huán)境中啟用緩存
isKeepAlive ?
<KeepAlive include={ cacheRoutes } keys={ cacheKeys }>
{ outlet } // 此處不能用 <Outlet /> 不然無法通過 useRef 來緩存
</KeepAlive> :
<TransitionGroup component={ null }>
<CSSTransition
key={ pathname + cacheKeys[pathname] }
appear={ true }
timeout={ 500 }
classNames='page-fade'>
{ outlet } // 此處不能用 <Outlet /> 會(huì)造成路由切換時(shí),組件重復(fù)渲染
</CSSTransition>
</TransitionGroup>
}
</section>
</Layout.Content>
)
}
Main.propTypes = {
fullScreen: PropTypes.bool,
cacheRoutes: PropTypes.array,
cacheKeys: PropTypes.object
}
const mapStateToProps = state => {
return {
fullScreen: state.setting.fullScreen,
cacheRoutes: state.tabsBar.cacheRoutes, // 需要緩存的路由組件path數(shù)組
cacheKeys: state.tabsBar.cacheKeys // 用于組件局部刷新
}
}
export default connect(mapStateToProps)(Main)
1、當(dāng)前圖片動(dòng)畫中,只設(shè)置了第二個(gè)標(biāo)簽頁緩存,其他的幾個(gè)未開啟緩存,所以可以看到,只有第二個(gè)標(biāo)簽頁在切回的時(shí)候 未重新請(qǐng)求數(shù)據(jù)。其他標(biāo)簽頁每次進(jìn)入都會(huì)重新請(qǐng)求數(shù)據(jù)。
2、此外這里結(jié)合react-transition-group 實(shí)現(xiàn)了路由切換的漸隱漸顯動(dòng)畫,但需要手動(dòng)配合css樣式控制,不能完全依靠CSSTransition
代碼部分:
// 路由進(jìn)入動(dòng)畫
.fade-enter, .fade-appear {
opacity: 0;
}
.fade-enter-active, .fade-appear-active {
opacity: 1;
@include transition(opacity .25s cubic-bezier(.645, .045, .355, 1) .25s);
}
.fade-exit {
opacity: 1;
z-index: 1;
}
.fade-exit-active {
opacity: 0;
z-index: 1;
@include transition(opacity .25s cubic-bezier(.645, .045, .355, 1));
}
.page-fade-enter, .page-fade-appear {
opacity: 0;
}
.page-fade-enter-active, .page-fade-appear-active {
opacity: 1;
@include transition(opacity .5s cubic-bezier(.645, .045, .355, 1));
}
.page-fade-exit {
opacity: 1;
display: none!important;
}
.page-fade-exit-active {
opacity: 0;
}
@keyframes keepAliveFade {
0% { opacity: 0; }
50% { opacity: 0; }
100% { opacity: 1; }
}
.layout-container {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
&.keep-alive-fade { animation: keepAliveFade .5s ease-in-out; }
}
到此這篇關(guān)于react-router-dom v6 通過outlet實(shí)現(xiàn)keepAlive 功能的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)react-router keepAlive 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
webpack-dev-server 的 host 配置 0.0.0.0的方法
這篇文章主要介紹了webpack-dev-server 的 host 配置 0.0.0.0的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,,需要的朋友可以參考下2024-01-01
JavaScript檢查某個(gè)function是否是原生代碼的方法
經(jīng)常碰到需要檢查某個(gè)function是否是原生代碼,要檢測(cè)這一點(diǎn),最簡(jiǎn)單的辦法當(dāng)然是判斷函數(shù)的 toString 方法返回的值2014-08-08
js去除重復(fù)字符串兩種實(shí)現(xiàn)方法
js去除重復(fù)字符串在項(xiàng)目開發(fā)中很實(shí)用,接下來詳細(xì)介紹實(shí)現(xiàn)方法,感興趣的朋友可以參考下2013-01-01
js實(shí)現(xiàn)商城星星評(píng)分的效果
這篇文章主要介紹了js實(shí)現(xiàn)商城星星評(píng)分的效果,很多網(wǎng)站都有如下圖這樣的星星打分效果,今天就看下用js怎么實(shí)現(xiàn)打分效果,需要的朋友可以參考下2015-12-12

