Commit 3085ccc1 by Wee

feat: implement `onHide` and `onReappear`

parent 3e00ae94
import { History } from 'history' import { History, Location } from 'history'
import * as React from 'react' import * as React from 'react'
import * as ReactDOM from 'react-dom' import * as ReactDOM from 'react-dom'
import { isValidElementType } from 'react-is' import { isValidElementType } from 'react-is'
...@@ -165,6 +165,9 @@ class LiveRoute extends React.Component<IProps, any> { ...@@ -165,6 +165,9 @@ class LiveRoute extends React.Component<IProps, any> {
sensitive = false, sensitive = false,
strict = false, strict = false,
history, history,
onReappear,
onHide,
forceUnmount,
location, location,
match: matchFromProps, match: matchFromProps,
path, path,
...@@ -177,23 +180,41 @@ class LiveRoute extends React.Component<IProps, any> { ...@@ -177,23 +180,41 @@ class LiveRoute extends React.Component<IProps, any> {
let { children } = this.props let { children } = this.props
const matchOfPath = matchPath((location as any).pathname, this.props) const matchOfPath = matchPath((location as any).pathname, this.props)
const matchOfLivePath = const matchOfLivePath = this.isLivePathMatch(livePath, location!.pathname, {
this.isLivePathMatch(livePath, location!.pathname, { path,
path, exact,
exact, strict,
strict, sensitive
sensitive })
}) || alwaysLive
const matchAnyway = matchOfPath || matchOfLivePath const matchAnyway = matchOfPath || matchOfLivePath
// normal render
if (matchOfPath) { if (matchOfPath) {
this.showRoute() this.showRoute()
this.restoreScrollPosition() this.restoreScrollPosition()
this.clearScroll() this.clearScroll()
// hide -> show
if (this.liveState === LiveState.HIDE_RENDER) {
if (typeof onReappear === 'function') {
onReappear(location!, matchAnyway, livePath, alwaysLive)
}
}
this.liveState = LiveState.NORMAL_RENDER_MATCHED
} }
// hide render
if (!matchOfPath && matchAnyway) { if (!matchOfPath && matchAnyway) {
this.saveScrollPosition() this.saveScrollPosition()
this.hideRoute() this.hideRoute()
// show -> hide
if (this.liveState === LiveState.NORMAL_RENDER_MATCHED) {
if (typeof onHide === 'function') {
onHide(location!, matchAnyway, livePath, alwaysLive)
}
}
this.liveState = LiveState.HIDE_RENDER
} }
const props = { ...staticContext, location, match: matchAnyway } const props = { ...staticContext, location, match: matchAnyway }
......
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