UNPKG

1.78 kBJavaScriptView Raw
1import React from 'react'
2import { Map } from 'immutable'
3import { connect } from 'react-redux'
4import { bindActionCreators } from 'redux'
5import * as actions from './action'
6import parseName from './parseName'
7import PropTypes from 'prop-types'
8
9class AppLoader extends React.Component {
10 constructor(props, context) {
11 super(props, context)
12 }
13
14 componentDidMount() {
15 const {
16 name: fullName,
17 payload
18 } = this.props
19
20 if (!payload.get('@@require')) {
21 this.props.loadApp(fullName)
22 }
23 }
24
25 componentWillReceiveProps(nextProps) {
26 const {
27 name: fullName,
28 payload
29 } = nextProps
30
31 if (!payload.get('@@require')) {
32 this.props.loadApp(fullName, this.props.name)
33 }
34 else if (this.props.name != nextProps.name) {
35 this.props.clearAppState(this.props.name)
36 }
37 }
38
39 //cxb效率优化点,由主动更新变更为状态比较更新?
40 shouldComponentUpdate(nextProps, nextState) {
41 return true
42 }
43
44 componentWillUnmount() {
45 const {
46 name: fullName,
47 payload
48 } = this.props
49
50 this.props.clearAppState(fullName)
51 }
52
53 render() {
54 const {
55 name: fullName,
56 payload,
57 ...other
58 } = this.props,
59
60 ReduxConnector = payload.getIn(['@@require', 'container'])
61
62 if (ReduxConnector) {
63 return (
64 <ReduxConnector
65 store={this.context.store}
66 {...other}
67 payload={payload}
68 key={fullName}
69 />
70 )
71
72 } else {
73 return null
74 }
75 }
76}
77
78AppLoader.contextTypes = {
79 store: PropTypes.object
80}
81
82export default connect((state, props) => {
83 const payload = state.get(props.name)
84
85 return {
86 payload: payload || Map()
87 }
88},
89 dispatch => ({
90 ...bindActionCreators(actions, dispatch)
91 }), null, {
92 withRef: true,
93 pure: true
94 }
95)(AppLoader)
\No newline at end of file