@@ -2,26 +2,62 @@ import React from 'react';
2
2
import request from 'superagent' ;
3
3
import PropTypes from 'prop-types' ;
4
4
5
- class Request extends React . Component {
5
+ export default class Request extends React . Component {
6
6
7
7
static defaultProps = {
8
8
method : 'get' ,
9
+ } ;
10
+
11
+ static propTypes = {
12
+ children : PropTypes . func ,
13
+ method : PropTypes . string . isRequired ,
14
+ type : PropTypes . string ,
15
+ accept : PropTypes . string ,
16
+ url : PropTypes . string . isRequired ,
17
+ timeout : PropTypes . number ,
18
+ verbose : PropTypes . bool ,
19
+ query : PropTypes . oneOfType ( [
20
+ PropTypes . string ,
21
+ PropTypes . object ,
22
+ ] ) ,
23
+ send : PropTypes . oneOfType ( [
24
+ PropTypes . string ,
25
+ PropTypes . object ,
26
+ ] ) ,
27
+ headers : PropTypes . object ,
28
+ auth : PropTypes . object ,
29
+ withCredentials : PropTypes . bool ,
30
+ buffer : PropTypes . bool ,
31
+ attach : PropTypes . array ,
32
+ fields : PropTypes . array ,
33
+ onRequest : PropTypes . func ,
34
+ } ;
35
+
36
+ request = null ;
37
+
38
+ state = {
39
+ error : null ,
40
+ result : null ,
41
+ loading : true ,
42
+ } ;
43
+
44
+ componentDidMount ( ) {
45
+ this . performRequest ( this . props ) ;
9
46
}
10
47
11
- constructor ( props ) {
12
- super ( props ) ;
13
- this . request = null ;
14
- this . state = {
15
- error : null ,
16
- result : null ,
17
- loading : true ,
18
- } ;
48
+ componentWillReceiveProps ( ...params ) {
49
+ this . willReceiveProps ( ...params ) ;
19
50
}
20
51
21
- componentWillMount ( ) {
22
- this . performRequest ( this . props ) ;
52
+ UNSAFE_componentWillReceiveProps ( ... params ) {
53
+ this . willReceiveProps ( ... params ) ;
23
54
}
24
- componentWillReceiveProps ( nextProps ) {
55
+
56
+ componentWillUnmount ( ) {
57
+ this . request . abort ( ) ;
58
+ }
59
+
60
+ willReceiveProps = nextProps => {
25
61
if ( JSON . stringify ( this . props ) === JSON . stringify ( nextProps ) ) {
26
62
return ;
27
63
}
@@ -36,10 +72,6 @@ class Request extends React.Component {
36
72
this . performRequest ( nextProps ) ;
37
73
}
38
74
39
- componentWillUnmount ( ) {
40
- this . request . abort ( ) ;
41
- }
42
-
43
75
performRequest ( props ) {
44
76
let { method } = props ;
45
77
if ( method === 'delete' ) {
@@ -102,17 +134,21 @@ class Request extends React.Component {
102
134
}
103
135
104
136
this . request
105
- . end ( ( error , result ) => {
106
- if ( error || ! result . ok ) {
107
- this . printLog ( props , error ) ;
108
- } else {
109
- this . printLog ( props , result ) ;
110
- }
137
+ . then ( result => {
138
+ this . printLog ( props , result ) ;
111
139
this . setState ( {
112
- error,
140
+ error : null ,
113
141
result,
114
142
loading : false ,
115
143
} ) ;
144
+ } )
145
+ . catch ( error => {
146
+ this . printLog ( props , error ) ;
147
+ this . setState ( {
148
+ error,
149
+ result : null ,
150
+ loading : false ,
151
+ } ) ;
116
152
} ) ;
117
153
}
118
154
@@ -126,30 +162,3 @@ class Request extends React.Component {
126
162
return this . props . children ( this . state ) ;
127
163
}
128
164
}
129
-
130
- Request . propTypes = {
131
- children : PropTypes . func ,
132
- method : PropTypes . string . isRequired ,
133
- type : PropTypes . string ,
134
- accept : PropTypes . string ,
135
- url : PropTypes . string . isRequired ,
136
- timeout : PropTypes . number ,
137
- verbose : PropTypes . bool ,
138
- query : PropTypes . oneOfType ( [
139
- PropTypes . string ,
140
- PropTypes . object ,
141
- ] ) ,
142
- send : PropTypes . oneOfType ( [
143
- PropTypes . string ,
144
- PropTypes . object ,
145
- ] ) ,
146
- headers : PropTypes . object ,
147
- auth : PropTypes . object ,
148
- withCredentials : PropTypes . bool ,
149
- buffer : PropTypes . bool ,
150
- attach : PropTypes . array ,
151
- fields : PropTypes . array ,
152
- onRequest : PropTypes . func ,
153
- } ;
154
-
155
- export default Request ;
0 commit comments