7
7
} catch ( e ) { }
8
8
9
9
const isFunction = arg => typeof arg === "function"
10
+ const renderFn = ( children , ...args ) =>
11
+ isFunction ( children ) ? children ( ...args ) : children === undefined ? null : children
10
12
11
13
/**
12
14
* createInstance allows you to create instances of Async that are bound to a specific promise.
@@ -186,12 +188,7 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
186
188
*/
187
189
const Initial = ( { children, persist } ) => (
188
190
< Consumer >
189
- { state => {
190
- if ( state . data !== undefined ) return null
191
- if ( ! persist && state . isPending ) return null
192
- if ( ! persist && state . error !== undefined ) return null
193
- return isFunction ( children ) ? children ( state ) : children || null
194
- } }
191
+ { state => ( state . isInitial || ( persist && ! state . data ) ? renderFn ( children , state ) : null ) }
195
192
</ Consumer >
196
193
)
197
194
@@ -206,15 +203,11 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
206
203
* Renders only while pending (promise is loading).
207
204
*
208
205
* @prop {Function|Node } children Function (passing state) or React node
209
- * @prop {boolean } initial Show only on initial load (data is undefined)
206
+ * @prop {boolean } initial Show only on initial load (data/error is undefined)
210
207
*/
211
208
const Pending = ( { children, initial } ) => (
212
209
< Consumer >
213
- { state => {
214
- if ( ! state . isPending ) return null
215
- if ( initial && state . data !== undefined ) return null
216
- return isFunction ( children ) ? children ( state ) : children || null
217
- } }
210
+ { state => ( state . isPending && ( ! initial || ! state . value ) ? renderFn ( children , state ) : null ) }
218
211
</ Consumer >
219
212
)
220
213
@@ -233,12 +226,9 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
233
226
*/
234
227
const Fulfilled = ( { children, persist } ) => (
235
228
< Consumer >
236
- { state => {
237
- if ( state . data === undefined ) return null
238
- if ( ! persist && state . isPending ) return null
239
- if ( ! persist && state . error !== undefined ) return null
240
- return isFunction ( children ) ? children ( state . data , state ) : children || null
241
- } }
229
+ { state =>
230
+ state . isFulfilled || ( persist && state . data ) ? renderFn ( children , state . data , state ) : null
231
+ }
242
232
</ Consumer >
243
233
)
244
234
@@ -257,11 +247,9 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
257
247
*/
258
248
const Rejected = ( { children, persist } ) => (
259
249
< Consumer >
260
- { state => {
261
- if ( state . error === undefined ) return null
262
- if ( state . isPending && ! persist ) return null
263
- return isFunction ( children ) ? children ( state . error , state ) : children || null
264
- } }
250
+ { state =>
251
+ state . isRejected || ( persist && state . error ) ? renderFn ( children , state . error , state ) : null
252
+ }
265
253
</ Consumer >
266
254
)
267
255
@@ -276,15 +264,11 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
276
264
* Renders only when promise is fulfilled or rejected.
277
265
*
278
266
* @prop {Function|Node } children Function (passing state) or React node
279
- * @prop {boolean } persist Show old data or error while pending (promise is loading)
267
+ * @prop {boolean } persist Continue rendering while loading new data
280
268
*/
281
269
const Settled = ( { children, persist } ) => (
282
270
< Consumer >
283
- { state => {
284
- if ( state . isInitial ) return null
285
- if ( state . isPending && ! persist ) return null
286
- return isFunction ( children ) ? children ( state ) : children || null
287
- } }
271
+ { state => ( state . isSettled || ( persist && state . value ) ? renderFn ( children , state ) : null ) }
288
272
</ Consumer >
289
273
)
290
274
0 commit comments