最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react-router-dom?v6?通過outlet實(shí)現(xiàn)keepAlive?功能的實(shí)現(xiàn)

 更新時(shí)間:2022年03月21日 10:36:08   作者:m0_67403240  
本文主要介紹了react-router-dom?v6?通過outlet實(shí)現(xiàn)keepAlive功能,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文主要介紹了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的方法

    這篇文章主要介紹了webpack-dev-server 的 host 配置 0.0.0.0的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,,需要的朋友可以參考下
    2024-01-01
  • JS跨域總結(jié)

    JS跨域總結(jié)

    JS跨域總結(jié),主要是解決js中跨域訪問的我問題
    2012-08-08
  • javascript 異常處理使用總結(jié)

    javascript 異常處理使用總結(jié)

    javascript 異常處理使用總結(jié)try…catch…finally window.onerror
    2009-06-06
  • JavaScript檢查某個(gè)function是否是原生代碼的方法

    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ù)字符串兩種實(shí)現(xiàn)方法

    js去除重復(fù)字符串在項(xiàng)目開發(fā)中很實(shí)用,接下來詳細(xì)介紹實(shí)現(xiàn)方法,感興趣的朋友可以參考下
    2013-01-01
  • 原生的html元素選擇器類似jquery選擇器

    原生的html元素選擇器類似jquery選擇器

    做前端,需要選擇元素,下面是實(shí)現(xiàn)代碼,一個(gè)原生的html元素選擇器類似jquery選擇器,很棒,很實(shí)用
    2014-10-10
  • js實(shí)現(xiàn)商城星星評(píng)分的效果

    js實(shí)現(xiàn)商城星星評(píng)分的效果

    這篇文章主要介紹了js實(shí)現(xiàn)商城星星評(píng)分的效果,很多網(wǎng)站都有如下圖這樣的星星打分效果,今天就看下用js怎么實(shí)現(xiàn)打分效果,需要的朋友可以參考下
    2015-12-12
  • JS+Canvas繪制時(shí)鐘效果

    JS+Canvas繪制時(shí)鐘效果

    這篇文章主要為大家詳細(xì)介紹了基于javascript下使用canvas繪制時(shí)鐘的具體實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • canvas 畫布在主流瀏覽器中的尺寸限制詳細(xì)介紹

    canvas 畫布在主流瀏覽器中的尺寸限制詳細(xì)介紹

    這篇文章主要介紹了canvas 畫布在主流瀏覽器中的尺寸限制詳細(xì)介紹的相關(guān)資料,canvas在不同瀏覽器下面有不同的最大尺寸限制,這里測(cè)試下,需要的朋友可以參考下
    2016-12-12
  • JavaScript中丟失精度的問題及避免方法

    JavaScript中丟失精度的問題及避免方法

    JavaScript?是一門動(dòng)態(tài)類型的腳本語言,用于在瀏覽器中創(chuàng)建交互式的網(wǎng)頁,然而,由于其使用?IEEE?754?浮點(diǎn)數(shù)表示數(shù)字,可能會(huì)導(dǎo)致丟失精度的問題,本文將探討?JavaScript?中的丟失精度問題,以及如何避免這些問題,需要的朋友可以參考下
    2023-11-11

最新評(píng)論

仙居县| 神木县| 巴里| 临沂市| 万宁市| 内乡县| 安丘市| 额敏县| 饶平县| 肥城市| 根河市| 麦盖提县| 嘉荫县| 滦平县| 平利县| 平乐县| 葫芦岛市| 准格尔旗| 工布江达县| 平安县| 丰城市| 双辽市| 长白| 兖州市| 甘德县| 彭山县| 仁化县| 崇文区| 宁远县| 连南| 南康市| 北辰区| 芜湖市| 自治县| 云林县| 海伦市| 应城市| 阿勒泰市| 五大连池市| 玉溪市| 浦东新区|