@@ -8,15 +8,26 @@ export const createInstance = (defaultProps = {}) => {
8
8
const { Consumer, Provider } = React . createContext ( )
9
9
10
10
class Async extends React . Component {
11
- mounted = false
12
- counter = 0
13
- args = [ ]
14
- state = {
15
- data : undefined ,
16
- error : undefined ,
17
- isLoading : false ,
18
- startedAt : undefined ,
19
- finishedAt : undefined
11
+ constructor ( props ) {
12
+ super ( props )
13
+ this . mounted = false
14
+ this . counter = 0
15
+ this . args = [ ]
16
+ this . state = {
17
+ data : undefined ,
18
+ error : undefined ,
19
+ isLoading : false ,
20
+ startedAt : undefined ,
21
+ finishedAt : undefined ,
22
+ cancel : this . cancel ,
23
+ run : this . run ,
24
+ reload : ( ) => {
25
+ this . load ( )
26
+ this . run ( ...this . args )
27
+ } ,
28
+ setData : this . setData ,
29
+ setError : this . setError
30
+ }
20
31
}
21
32
22
33
componentDidMount ( ) {
@@ -83,27 +94,12 @@ export const createInstance = (defaultProps = {}) => {
83
94
84
95
render ( ) {
85
96
const { children } = this . props
86
-
87
- const renderProps = {
88
- ...this . state ,
89
- cancel : this . cancel ,
90
- run : this . run ,
91
- reload : ( ) => {
92
- this . load ( )
93
- this . run ( ...this . args )
94
- } ,
95
- setData : this . setData ,
96
- setError : this . setError
97
- }
98
-
99
97
if ( typeof children === "function" ) {
100
- return < Provider value = { renderProps } > { children ( renderProps ) } </ Provider >
98
+ return < Provider value = { this . state } > { children ( this . state ) } </ Provider >
101
99
}
102
-
103
100
if ( children !== undefined && children !== null ) {
104
- return < Provider value = { renderProps } > { children } </ Provider >
101
+ return < Provider value = { this . state } > { children } </ Provider >
105
102
}
106
-
107
103
return null
108
104
}
109
105
}
@@ -112,15 +108,15 @@ export const createInstance = (defaultProps = {}) => {
112
108
* Renders only when deferred promise is pending (not yet run).
113
109
*
114
110
* @prop {boolean } persist Show until we have data, even while loading or when an error occurred
115
- * @prop {Function|Node } children Function (passing props ) or React node
111
+ * @prop {Function|Node } children Function (passing state ) or React node
116
112
*/
117
113
Async . Pending = ( { children, persist } ) => (
118
114
< Consumer >
119
- { props => {
120
- if ( props . data !== undefined ) return null
121
- if ( ! persist && props . isLoading ) return null
122
- if ( ! persist && props . error !== undefined ) return null
123
- return typeof children === "function" ? children ( props ) : children || null
115
+ { state => {
116
+ if ( state . data !== undefined ) return null
117
+ if ( ! persist && state . isLoading ) return null
118
+ if ( ! persist && state . error !== undefined ) return null
119
+ return typeof children === "function" ? children ( state ) : children || null
124
120
} }
125
121
</ Consumer >
126
122
)
@@ -129,14 +125,14 @@ export const createInstance = (defaultProps = {}) => {
129
125
* Renders only while loading.
130
126
*
131
127
* @prop {boolean } initial Show only on initial load (data is undefined)
132
- * @prop {Function|Node } children Function (passing props ) or React node
128
+ * @prop {Function|Node } children Function (passing state ) or React node
133
129
*/
134
130
Async . Loading = ( { children, initial } ) => (
135
131
< Consumer >
136
- { props => {
137
- if ( ! props . isLoading ) return null
138
- if ( initial && props . data !== undefined ) return null
139
- return typeof children === "function" ? children ( props ) : children || null
132
+ { state => {
133
+ if ( ! state . isLoading ) return null
134
+ if ( initial && state . data !== undefined ) return null
135
+ return typeof children === "function" ? children ( state ) : children || null
140
136
} }
141
137
</ Consumer >
142
138
)
@@ -145,14 +141,14 @@ export const createInstance = (defaultProps = {}) => {
145
141
* Renders only when promise is resolved.
146
142
*
147
143
* @prop {boolean } persist Show old data while loading
148
- * @prop {Function|Node } children Function (passing data and props ) or React node
144
+ * @prop {Function|Node } children Function (passing data and state ) or React node
149
145
*/
150
146
Async . Resolved = ( { children, persist } ) => (
151
147
< Consumer >
152
- { props => {
153
- if ( props . data === undefined ) return null
154
- if ( props . isLoading && ! persist ) return null
155
- return typeof children === "function" ? children ( props . data , props ) : children || null
148
+ { state => {
149
+ if ( state . data === undefined ) return null
150
+ if ( state . isLoading && ! persist ) return null
151
+ return typeof children === "function" ? children ( state . data , state ) : children || null
156
152
} }
157
153
</ Consumer >
158
154
)
@@ -161,14 +157,14 @@ export const createInstance = (defaultProps = {}) => {
161
157
* Renders only when promise is rejected.
162
158
*
163
159
* @prop {boolean } persist Show old error while loading
164
- * @prop {Function|Node } children Function (passing error and props ) or React node
160
+ * @prop {Function|Node } children Function (passing error and state ) or React node
165
161
*/
166
162
Async . Rejected = ( { children, persist } ) => (
167
163
< Consumer >
168
- { props => {
169
- if ( props . error === undefined ) return null
170
- if ( props . isLoading && ! persist ) return null
171
- return typeof children === "function" ? children ( props . error , props ) : children || null
164
+ { state => {
165
+ if ( state . error === undefined ) return null
166
+ if ( state . isLoading && ! persist ) return null
167
+ return typeof children === "function" ? children ( state . error , state ) : children || null
172
168
} }
173
169
</ Consumer >
174
170
)
0 commit comments