diff --git a/src/keep-alive/withKeepAlive.js b/src/keep-alive/withKeepAlive.js index aae1fe2..edc0167 100644 --- a/src/keep-alive/withKeepAlive.js +++ b/src/keep-alive/withKeepAlive.js @@ -1,22 +1,24 @@ import React, { useContext, useRef, useEffect } from 'react' import CacheContext from './CacheContext' import * as cacheTypes from './cache-types' -const withKeepAlive = (OldComponent, { cacheId = window.location.pathname }) => { +const withKeepAlive = (OldComponent) => { const keepAliveWrapper = (props) => { const { mount, cacheStates, dispatch } = useContext(CacheContext) const ref = useRef(null) useEffect(() => { - const cacheState = cacheStates[cacheId] + const cacheState = cacheStates[props.match.url] if (cacheState && cacheState.doms && cacheState.status !== cacheTypes.DESTROY) { const doms = cacheState.doms + // 先清空 childNodes 再注入 + ref.current.childNodes.forEach((child) => ref.current.removeChild(child)) doms.forEach((dom) => ref.current.appendChild(dom)) } else { - mount({ cacheId, element: }) + mount({ cacheId: props.match.url, element: }) } }, [cacheStates, dispatch, mount, props]) - return
+ return
} return keepAliveWrapper } diff --git a/src/util/common.js b/src/util/common.js index 9411648..9ffb963 100644 --- a/src/util/common.js +++ b/src/util/common.js @@ -172,7 +172,7 @@ export const existKeepAliveRouter = (routes, withKeepAlive) => { const { path, component, children, keepAlive, scroll } = router isExist = isExist || keepAlive if (keepAlive && path) { - router.component = withKeepAlive(component, { cacheId: path, scroll }) + router.component = withKeepAlive(component, { scroll }) } if (children) { getKeepAlive(children)