日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

React-Route6實現keep-alive效果_React

作者:小鵬愛摸魚 ? 更新時間: 2022-08-17 編程語言

一、基于react-route6??useOutlet實現

二、代碼呈現

import React, { useRef, createContext, useContext } from 'react'
import { useOutlet, useLocation, matchPath } from 'react-router-dom'
import type { FC } from 'react'
//在組件外部建立一個Context
export const KeepAliveContext = createContext<KeepAliveLayoutProps>({ keepalive: [], keepElements: {} })

//給予頁面緩存設置條件判斷
const isKeepPath = (aliveList: any[], path: string) => {
  let isKeep = false
  aliveList.map(item => {
    if (item === path) {
      isKeep = true
    }
    if (item instanceof RegExp && item.test(path)) {
      isKeep = true
    } 
  })
  return isKeep
}
//判斷當前頁面是否已緩存,是則控制hidden開關顯示 ,不是則正常渲染
export function useKeepOutlets() {
  const location = useLocation()
  const element = useOutlet()
  const { keepElements, keepalive } = useContext<any>(KeepAliveContext)
  const isKeep = isKeepPath(keepalive, location.pathname)
  if (isKeep) {
    keepElements.current[location.pathname] = element
  }
  //標簽的顯示與隱藏
  return <> {
    Object.entries(keepElements.current).map(([pathname, element]: any) => (
      <div key={pathname} 
      style={{ height: '100%', width: '100%', position: 'relative', overflow: 'hidden auto' }}       className="rumtime-keep-alive-layout" 
      hidden={!matchPath(location.pathname, pathname)}>
        {element}
      </div>
    ))
  }
    <div hidden={isKeep} style={{ height: '100%', width: '100%', position: 'relative', overflow: 'hidden auto' }} className="rumtime-keep-alive-layout-no">
      {!isKeep && element}
    </div>
  </>
}
//設置公共組件類型
interface KeepAliveLayoutProps {
  keepalive: any[]
  keepElements?: any
  dropByCacheKey?: (path: string) => void 
}
//封裝公共組件
const KeepAliveLayout: FC<KeepAliveLayoutProps> = (props) => { 
  const { keepalive, ...other } = props
  const keepElements = React.useRef<any>({}) 
  function dropByCacheKey(path: string) { 
    keepElements.current[path] = null
   } return (<KeepAliveContext.Provider 
    value={{ keepalive, keepElements, dropByCacheKey }}
     {...other} />) 
    }
  
  export default KeepAliveLayout

代碼分析

isKeepPath

配置 keepalive 支持字符串和正則,通過它來判斷,當前頁面是否需要狀態保持,因為如果整個項目的頁面都保持狀態的話,對性能是很大的消耗

參數1為可緩存路徑或正則表達式組成的數組,參數2為當前路徑。

若當前路徑在已緩存路徑數組中或其路徑符合正則表達式則isKeep為true,反之為false

useKeepOutlets

通過判斷當前頁面是否是需要保持的頁面來對頁面 DOM 做一個 hidden 顯隱開關。

需要注意的是所有被指定狀態保持的頁面在首次渲染之后,都會被掛載在頁面 DOM 樹上,僅僅是使用 !matchPath(location.pathname, pathname) 控制顯隱。

而沒有被指定狀態保持的頁面,則是使用 {!isKeep && element} 控制,走 React 組件正常的生命周期。

location

當前路徑信息

element

獲取當前路由組件即當前配置下的嵌套路由組件

useContext<any>(KeepAliveContext)

通過useContext()鉤子函數獲取Context對象中的屬性,已便于組件之間共享狀態

isKeep

將當前路徑利用頁面緩存設置條件判斷是否為已緩存路徑,若符合條件,isKeep為true,則將keepElements Ref中以當前組件路徑名為屬性名的屬性綁定當前路由組件

Object.entries

描述:Object.entries()返回一個數組,其元素是與直接在object上找到的可枚舉屬性鍵值對相對應的數組。屬性的順序與通過手動循環對象的屬性值所給出的順序相同。

可枚舉:枚舉的功能類似于字面量類型+聯合類型組合的功能,也可以表示一組明確的可選值

參數:可以返回其可枚舉屬性的鍵值對的對象

返回值:給定對象自身可枚舉屬性的鍵值對數組

key

保持 key 不變,就不會觸發 React 的重繪

hidden

控制顯示與隱藏開關

matchPath(location.pathname, pathname)}

所有被指定狀態保持的頁面在首次渲染之后,都會被掛載在頁面 DOM 樹上,僅僅是使用!matchPath(location.pathname, pathname) 控制顯示隱藏。

//matchPath:參數1為當前路徑,參數2為緩存路徑,確定當前路由路徑是否與緩存路徑匹配

而沒有被指定狀態保持的頁面,則是使用 {!isKeep && element} 控制,走 React 組件正常的生命周期

KeepAliveLayout

為封裝后暴露的組件

FC

React.FC是函數式組件,是在TypeScript下使用的一個泛型,全稱為React.FunctionComponentReact.FC<>?可檢測指定屬性類型

keepElements

使用?React.useRef<any>({})?來做頁面數據保存的節點,是因為我們的上下文不被重新渲染的話?keepElements?就不會被重置,相當于?key?

dropByCacheKey

dropByCacheKey為清除緩存的函數,通過控制當前組件的ref來銷毀組件

other

const { keepalive, ...other } = props中...other為其他配置,這里我們直接遍歷繼承即可

Provider

(<KeepAliveContext.Provider value={{ keepalive, keepElements, dropByCacheKey }} {...other} />)

原理:

每個 Context 對象都會返回一個 Provider React 組件,它允許消費組件訂閱 context 的變化。

Provider 接收一個?value?屬性,傳遞給消費組件。一個 Provider 可以和多個消費組件有對應關系。多個 Provider 也可以嵌套使用,里層的會覆蓋外層的數據。

當 Provider 的?value?值發生變化時,它內部的所有消費組件都會重新渲染

三、使用

import KeepAliveLayout, { useKeepOutlets, KeepAliveContext }from'@/components/KeepAliveLayout'
import { useLocation } from 'react-router-dom'
import React, { useState, useContext } from 'react'

// 使用KeepAliveLayout中的useKeepOutlets獲取當前渲染的頁面內容
const Layout = () => {
    const element = useKeepOutlets()
    return (
        {element}
    )
}
// 使用 KeepAliveLayout 包裹上下文
const App = () => {
    return (
        <KeepAliveLayout keepalive={[/./]}>//不可能組件都緩存吧所以需要設置緩存條件,可傳可緩存的路徑或正則表達式
            // App
        </KeepAliveLayout>
    );
}
// 使用 useContext 獲取 dropByCacheKey 清除緩存
const Home = () => {
    const { dropByCacheKey } = useContext<any>(KeepAliveContext);
    const { pathname } = useLocation();
    return (
       <button onClick={() => dropByCacheKey(pathname)}>清除緩存</button>
    )
}

原文鏈接:https://juejin.cn/post/7112316129645133838

欄目分類
最近更新