UNPKG

2.62 kBJavaScriptView Raw
1import React, { Component } from 'react'
2import ReactDOM from 'react-dom'
3import monkeyKing from './monkeyKing'
4import config from './config'
5import utils from 'edf-utils'
6
7export default function wrapper(option) {
8 return WrappedComponent => {
9 return class internal extends Component {
10
11 constructor(props) {
12 super(props)
13 this.state = { hasError: false }
14 }
15
16 componentWillMount() {
17 this.props.componentWillMount && this.props.componentWillMount()
18 }
19
20 componentDidMount() {
21 this.props.initView && this.props.initView(this) //兼容以前版本
22 this.props.componentDidMount && this.props.componentDidMount()
23 }
24
25 shouldComponentUpdate(nextProps, nextState) {
26 if (this.props.shouldComponentUpdate
27 && this.props.shouldComponentUpdate(nextProps, nextState) === true)
28 return true
29
30 if (nextState.hasError != this.state.hasError) {
31 return true
32 }
33
34 for (var o in this.props) {
35 if (this.props[o] != nextProps[o]) {
36 return true
37 }
38 }
39 return false
40 }
41
42
43 componentWillReceiveProps(nextProps) {
44 if (this.state.hasError) {
45 this.setState({ hasError: false, error: undefined })
46 }
47
48 this.props.componentWillReceiveProps
49 && this.props.componentWillReceiveProps(nextProps)
50 }
51
52 componentWillUpdate(nextProps, nextState) {
53 this.props.componentWillUpdate
54 && this.props.componentWillUpdate(nextProps, nextState)
55 }
56
57 componentDidCatch(error, info) {
58 utils.exception.error(error)
59 this.setState({ hasError: true, error })
60
61 this.props.componentDidCatch
62 && this.props.componentDidCatch(error, info)
63 }
64
65
66 componentWillUnmount() {
67 this.props.unmount && this.props.unmount() //兼容以前版本
68 this.props.componentWillUnmount
69 && this.props.componentWillUnmount()
70 }
71
72 componentDidUpdate() {
73 this.props.componentDidUpdate
74 && this.props.componentDidUpdate()
75 }
76
77 render() {
78 if (this.state.hasError) {
79 //return <div style={{ color: 'red' }}>{this.state.error}</div>
80 return <div style={{ color: 'red' }}>{this.state.error && this.state.error.message }</div>
81 }
82
83 if (this.props.notRender === true || this.props._notRender === true)
84 return null
85
86 if (!WrappedComponent)
87 return null
88
89 if (!this.props.payload || !this.props.payload.get('data'))
90 return null
91
92 if (this.props.payload.getIn(['data', '_notRender']) === true)
93 return null
94
95 return <WrappedComponent {...this.props} monkeyKing={monkeyKing} />
96 }
97 }
98 }
99}
100
101
\No newline at end of file