1
1
import React from "react"
2
2
3
+ const isFunction = arg => typeof arg === "function"
4
+
3
5
/**
4
6
* createInstance allows you to create instances of Async that are bound to a specific promise.
5
7
* A unique instance also uses its own React context for better nesting capability.
@@ -16,7 +18,7 @@ export const createInstance = (defaultProps = {}) => {
16
18
this . state = {
17
19
data : undefined ,
18
20
error : undefined ,
19
- isLoading : false ,
21
+ isLoading : isFunction ( props . promiseFn ) || isFunction ( defaultProps . promiseFn ) ,
20
22
startedAt : undefined ,
21
23
finishedAt : undefined ,
22
24
cancel : this . cancel ,
@@ -94,7 +96,7 @@ export const createInstance = (defaultProps = {}) => {
94
96
95
97
render ( ) {
96
98
const { children } = this . props
97
- if ( typeof children === "function" ) {
99
+ if ( isFunction ( children ) ) {
98
100
return < Provider value = { this . state } > { children ( this . state ) } </ Provider >
99
101
}
100
102
if ( children !== undefined && children !== null ) {
@@ -116,7 +118,7 @@ export const createInstance = (defaultProps = {}) => {
116
118
if ( state . data !== undefined ) return null
117
119
if ( ! persist && state . isLoading ) return null
118
120
if ( ! persist && state . error !== undefined ) return null
119
- return typeof children === "function" ? children ( state ) : children || null
121
+ return isFunction ( children ) ? children ( state ) : children || null
120
122
} }
121
123
</ Consumer >
122
124
)
@@ -132,7 +134,7 @@ export const createInstance = (defaultProps = {}) => {
132
134
{ state => {
133
135
if ( ! state . isLoading ) return null
134
136
if ( initial && state . data !== undefined ) return null
135
- return typeof children === "function" ? children ( state ) : children || null
137
+ return isFunction ( children ) ? children ( state ) : children || null
136
138
} }
137
139
</ Consumer >
138
140
)
@@ -148,7 +150,7 @@ export const createInstance = (defaultProps = {}) => {
148
150
{ state => {
149
151
if ( state . data === undefined ) return null
150
152
if ( state . isLoading && ! persist ) return null
151
- return typeof children === "function" ? children ( state . data , state ) : children || null
153
+ return isFunction ( children ) ? children ( state . data , state ) : children || null
152
154
} }
153
155
</ Consumer >
154
156
)
@@ -164,7 +166,7 @@ export const createInstance = (defaultProps = {}) => {
164
166
{ state => {
165
167
if ( state . error === undefined ) return null
166
168
if ( state . isLoading && ! persist ) return null
167
- return typeof children === "function" ? children ( state . error , state ) : children || null
169
+ return isFunction ( children ) ? children ( state . error , state ) : children || null
168
170
} }
169
171
</ Consumer >
170
172
)
0 commit comments