2016-10-05 23:52:50 +00:00
|
|
|
import React, { Component, PropTypes } from 'react'
|
|
|
|
|
|
|
|
export default class App extends Component {
|
|
|
|
static childContextTypes = {
|
|
|
|
router: PropTypes.object,
|
|
|
|
headManager: PropTypes.object
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
|
|
|
this.state = propsToState(props)
|
|
|
|
this.close = null
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
const state = propsToState(nextProps)
|
|
|
|
try {
|
|
|
|
this.setState(state)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
const { router } = this.props
|
|
|
|
|
2016-10-08 05:12:51 +00:00
|
|
|
this.close = router.subscribe((data) => {
|
|
|
|
const props = data.props || this.state.props
|
2016-10-05 23:52:50 +00:00
|
|
|
const state = propsToState({
|
2016-10-08 05:12:51 +00:00
|
|
|
...data,
|
2016-10-16 05:25:13 +00:00
|
|
|
props,
|
2016-10-06 07:08:23 +00:00
|
|
|
router
|
2016-10-05 23:52:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.setState(state)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
if (this.close) this.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
getChildContext () {
|
|
|
|
const { router, headManager } = this.props
|
|
|
|
return { router, headManager }
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { Component, props } = this.state
|
|
|
|
return React.createElement(Component, { ...props })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function propsToState (props) {
|
|
|
|
const { Component, router } = props
|
2016-10-08 05:12:51 +00:00
|
|
|
const { route } = router
|
2016-10-05 23:52:50 +00:00
|
|
|
const url = {
|
|
|
|
query: router.query,
|
|
|
|
pathname: router.pathname,
|
|
|
|
back: () => router.back(),
|
2016-10-08 05:12:51 +00:00
|
|
|
push: (url) => router.push(route, url),
|
|
|
|
pushTo: (url) => router.push(null, url),
|
|
|
|
replace: (url) => router.replace(route, url),
|
|
|
|
replaceTo: (url) => router.replace(null, url)
|
2016-10-05 23:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
Component,
|
|
|
|
props: { ...props.props, url }
|
|
|
|
}
|
|
|
|
}
|