Commit 961c21a6 by Wee

fix: LiveRoute hide component when directly enter livePath Route. It should be…

fix: LiveRoute hide component when directly enter livePath Route. It should be unmatched normal render.
parent aa705d1d
...@@ -6,9 +6,9 @@ import matchPath from './matchPath' ...@@ -6,9 +6,9 @@ import matchPath from './matchPath'
import ReactDOM from 'react-dom' import ReactDOM from 'react-dom'
const isEmptyChildren = children => React.Children.count(children) === 0 const isEmptyChildren = children => React.Children.count(children) === 0
const NORMAL_RENDER_MATCH = 'render the component as Route when path is matched' const NORMAL_RENDER_MATCH = 'normal matched render'
const NORMAL_RENDER_UNMATCH = 'unmount the component as Route when path is not matched' const NORMAL_RENDER_UNMATCH = 'normal unmatched render(unmount)'
const HIDE_RENDER = 'hide the component of LiveRoute when livePath is matched and previous location is matched' const HIDE_RENDER = 'hide route when matched'
/** /**
* The public API for matching a single path and rendering. * The public API for matching a single path and rendering.
*/ */
...@@ -139,6 +139,7 @@ class Route extends React.Component { ...@@ -139,6 +139,7 @@ class Route extends React.Component {
componentWillUnmount() { componentWillUnmount() {
if (this.doesRouteEnableLive()) { if (this.doesRouteEnableLive()) {
window.sessionStorage.removeItem(this.routeId) window.sessionStorage.removeItem(this.routeId)
this.routeDom = null
} }
} }
...@@ -162,7 +163,7 @@ class Route extends React.Component { ...@@ -162,7 +163,7 @@ class Route extends React.Component {
* 在每次隐藏渲染的时候,都返回一个 prevMatch,等到渲染的时候,它拿这次的 prevMatch 和之前的 router 去最终渲染。 * 在每次隐藏渲染的时候,都返回一个 prevMatch,等到渲染的时候,它拿这次的 prevMatch 和之前的 router 去最终渲染。
*/ */
computeMatchWithLive(props, nextProps, nextContext, match) { computeMatchWithLive(props, nextProps, nextContext, match) {
console.log('进入 livePath') console.log(`>>>>>> ` + this.props.name + ` <<<<<`)
console.log(this._routeInited) console.log(this._routeInited)
// 计算 livePath 是否匹配 // 计算 livePath 是否匹配
const livePath = nextProps.livePath const livePath = nextProps.livePath
...@@ -170,15 +171,17 @@ class Route extends React.Component { ...@@ -170,15 +171,17 @@ class Route extends React.Component {
const prevMatch = this.computeMatch(props, this.context.router) const prevMatch = this.computeMatch(props, this.context.router)
const livePathMatch = this.computeMatch(nextPropsWithLivePath, nextContext.router) const livePathMatch = this.computeMatch(nextPropsWithLivePath, nextContext.router)
if (match || (props.alwaysLive && !this._routeInited)) { if (match || (props.alwaysLive && !this._routeInited)) {
console.log('------- NORMAL MATCH FLAG -------') // 匹配正常渲染
// 正常存活 console.log('---- NORMAL MATCH FLAG ----')
this.liveState = NORMAL_RENDER_MATCH this.liveState = NORMAL_RENDER_MATCH
this._prevRouter = this.context.router this._prevRouter = this.context.router
return match return match
} else if ((livePathMatch && prevMatch) || props.alwaysLive) { } else if ((livePathMatch || props.alwaysLive) && this.routeDom) {
// 备份一下需要渲染的参数 // 隐藏渲染
console.log('------- HIDE FLAG-------') console.log('---- HIDE FLAG----')
this.liveState = HIDE_RENDER this.liveState = HIDE_RENDER
this.saveScroll()
this.hideRoute()
if (prevMatch) { if (prevMatch) {
this._prevMatch = prevMatch this._prevMatch = prevMatch
return prevMatch return prevMatch
...@@ -186,14 +189,15 @@ class Route extends React.Component { ...@@ -186,14 +189,15 @@ class Route extends React.Component {
return this._prevMatch return this._prevMatch
} }
} else { } else {
// 不匹配正常卸载
console.log('---- NORMAL UNMATCH FLAG----')
this.liveState = NORMAL_RENDER_UNMATCH this.liveState = NORMAL_RENDER_UNMATCH
console.log('------- NORMAL UNMATCH FLAG -------') this.routeDom = null
window.sessionStorage.removeItem(this.routeId)
} }
} }
// Route 中正常计算 match
computeMatch({ computedMatch, location, path, strict, exact, sensitive }, router) { computeMatch({ computedMatch, location, path, strict, exact, sensitive }, router) {
// react-live-route: ignore match from <Switch>, actually LiveRoute should not be wrapped by <Switch>.
// if (computedMatch) return computedMatch // <Switch> already computed the match for us // if (computedMatch) return computedMatch // <Switch> already computed the match for us
invariant(router, 'You should not use <Route> or withRouter() outside a <Router>') invariant(router, 'You should not use <Route> or withRouter() outside a <Router>')
...@@ -263,7 +267,6 @@ class Route extends React.Component { ...@@ -263,7 +267,6 @@ class Route extends React.Component {
// 正常渲染 component 或 render // 正常渲染 component 或 render
renderRoute(component, render, props, match) { renderRoute(component, render, props, match) {
console.log(`>>>` + this.props.name)
console.log(match) console.log(match)
if (component) return match ? React.createElement(component, props) : null if (component) return match ? React.createElement(component, props) : null
if (render) return match ? render(props) : null if (render) return match ? render(props) : null
...@@ -284,12 +287,10 @@ class Route extends React.Component { ...@@ -284,12 +287,10 @@ class Route extends React.Component {
} else if (this.liveState === HIDE_RENDER) { } else if (this.liveState === HIDE_RENDER) {
// 隐藏渲染 // 隐藏渲染
const prevRouter = this._prevRouter const prevRouter = this._prevRouter
// 取出 prevRouter 中的属性 // 取出 prevRouter 中的属性 mock 出上次正常渲染的 props
const { history, route, staticContext } = prevRouter const { history, route, staticContext } = prevRouter
const location = this.props.location || route.location const location = this.props.location || route.location
const liveProps = { match, location, history, staticContext } const liveProps = { match, location, history, staticContext }
this.saveScroll()
this.hideRoute()
return this.renderRoute(component, render, liveProps, true) return this.renderRoute(component, render, liveProps, true)
} }
} }
......
{ {
"name": "react-live-route", "name": "react-live-route",
"version": "1.1.9", "version": "1.1.10",
"description": "A living route for react-router-v4", "description": "A living route for react-router-v4",
"repository": "fi3ework/react-live-route", "repository": "fi3ework/react-live-route",
"license": "MIT", "license": "MIT",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment