Commit de1c7e7c by Wee

fix(showRoute): move showRoute to componentDidUpdate

parent c712fcdd
...@@ -6,8 +6,18 @@ An enhanced version of react-router-v4 **Route** Component that keeps route comp ...@@ -6,8 +6,18 @@ An enhanced version of react-router-v4 **Route** Component that keeps route comp
## Demo ## Demo
### codeSandbox
You can experience react-live-route on codeSandbox.
[![Edit react-live-route-demo-1](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/yj9j33pw4j) [![Edit react-live-route-demo-1](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/yj9j33pw4j)
### QR code
You also can scan the QR code of the demo above to experience it on mobile device.
![qr](./docs/qr.png)
## Install ## Install
```bash ```bash
......
docs/qr.png

22.8 KB

...@@ -7,6 +7,8 @@ import ReactDOM from 'react-dom' ...@@ -7,6 +7,8 @@ import ReactDOM from 'react-dom'
const isEmptyChildren = children => React.Children.count(children) === 0 const isEmptyChildren = children => React.Children.count(children) === 0
const NORMAL_RENDER = 0 const NORMAL_RENDER = 0
const NORMAL_RENDER_MATCH = 2
const NORMAL_RENDER_UNMATCH = 3
const HIDE_RENDER = 1 const HIDE_RENDER = 1
/** /**
* The public API for matching a single path and rendering. * The public API for matching a single path and rendering.
...@@ -43,7 +45,7 @@ class Route extends React.Component { ...@@ -43,7 +45,7 @@ class Route extends React.Component {
if (props.alwaysLive) { if (props.alwaysLive) {
this._routeInited = false this._routeInited = false
} }
this.liveState = NORMAL_RENDER this.liveState = NORMAL_RENDER_MATCH
} }
getChildContext() { getChildContext() {
...@@ -126,6 +128,11 @@ class Route extends React.Component { ...@@ -126,6 +128,11 @@ class Route extends React.Component {
console.log('--- inited ---') console.log('--- inited ---')
} }
} }
if (this.liveState === NORMAL_RENDER_MATCH) {
// 正常渲染
this.showRoute()
}
} }
/** /**
...@@ -152,9 +159,9 @@ class Route extends React.Component { ...@@ -152,9 +159,9 @@ 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 FLAG -------') console.log('------- NORMAL MATCH FLAG -------')
// 正常存活 // 正常存活
this.liveState = NORMAL_RENDER this.liveState = NORMAL_RENDER_MATCH
this._prevRouter = this.context.router this._prevRouter = this.context.router
return match return match
} else if (livePathMatch || props.alwaysLive) { } else if (livePathMatch || props.alwaysLive) {
...@@ -168,8 +175,9 @@ class Route extends React.Component { ...@@ -168,8 +175,9 @@ class Route extends React.Component {
return this._prevMatch return this._prevMatch
} }
} else { } else {
this.liveState = NORMAL_RENDER this.liveState = NORMAL_RENDER_UNMATCH
console.log('------- NO MATCH FLAG -------') console.log('------- NORMAL UNMATCH FLAG -------')
window.sessionStorage.removeItem(this.getComposeRouteId())
} }
} }
...@@ -192,7 +200,7 @@ class Route extends React.Component { ...@@ -192,7 +200,7 @@ class Route extends React.Component {
} }
// compose LiveRoute uuid // compose LiveRoute uuid
composeRouteId() { getComposeRouteId() {
let id = '' let id = ''
if (id) { if (id) {
return id return id
...@@ -207,11 +215,10 @@ class Route extends React.Component { ...@@ -207,11 +215,10 @@ class Route extends React.Component {
// 隐藏 DOM 并保存 scroll // 隐藏 DOM 并保存 scroll
hideRoute() { hideRoute() {
const previousDisplayStyle = this.routeDom.style.display console.log('--- hide route ---')
if (this.routeDom && previousDisplayStyle !== 'none') { if (this.routeDom && this.routeDom.style.display !== 'none') {
console.log(this.props)
this.saveScroll() this.saveScroll()
this._previousDisplayStyle = previousDisplayStyle this._previousDisplayStyle = this.routeDom.style.display
this.routeDom.style.display = 'none' this.routeDom.style.display = 'none'
} }
} }
...@@ -228,12 +235,13 @@ class Route extends React.Component { ...@@ -228,12 +235,13 @@ class Route extends React.Component {
saveScroll = () => { saveScroll = () => {
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
const scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft const scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft
window.sessionStorage.setItem(this.composeRouteId(), JSON.stringify({ top: scrollTop, left: scrollLeft })) console.log(`saved top = ${scrollTop}, left = ${scrollLeft}`)
window.sessionStorage.setItem(this.getComposeRouteId(), JSON.stringify({ top: scrollTop, left: scrollLeft }))
} }
// 恢复离开前的 scroll // 恢复离开前的 scroll
restoreScroll = () => { restoreScroll = () => {
const scroll = JSON.parse(window.sessionStorage.getItem(this.composeRouteId())) const scroll = JSON.parse(window.sessionStorage.getItem(this.getComposeRouteId()))
console.log(scroll) console.log(scroll)
if (scroll && typeof scroll.top === 'number') { if (scroll && typeof scroll.top === 'number') {
window.scroll({ top: scroll.top, left: scroll.left }) window.scroll({ top: scroll.top, left: scroll.left })
...@@ -255,9 +263,8 @@ class Route extends React.Component { ...@@ -255,9 +263,8 @@ class Route extends React.Component {
if ((livePath || alwaysLive) && (component || render)) { if ((livePath || alwaysLive) && (component || render)) {
console.log('=== RENDER FLAG: ' + this.liveState + ' ===') console.log('=== RENDER FLAG: ' + this.liveState + ' ===')
if (this.liveState === NORMAL_RENDER) { if (this.liveState === NORMAL_RENDER_MATCH || this.liveState === NORMAL_RENDER_UNMATCH) {
// 正常渲染 // 正常渲染
this.showRoute()
return this.renderRoute(component, render, props, match) return this.renderRoute(component, render, props, match)
} else if (this.liveState === HIDE_RENDER) { } else if (this.liveState === HIDE_RENDER) {
// 隐藏渲染 // 隐藏渲染
......
{ {
"name": "react-live-route", "name": "react-live-route",
"version": "1.1.6", "version": "1.1.7",
"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