11import React from "react"
22
3+ const isFunction = arg => typeof arg === "function"
4+
35/**
46 * createInstance allows you to create instances of Async that are bound to a specific promise.
57 * A unique instance also uses its own React context for better nesting capability.
@@ -16,7 +18,7 @@ export const createInstance = (defaultProps = {}) => {
1618 this . state = {
1719 data : undefined ,
1820 error : undefined ,
19- isLoading : false ,
21+ isLoading : isFunction ( props . promiseFn ) || isFunction ( defaultProps . promiseFn ) ,
2022 startedAt : undefined ,
2123 finishedAt : undefined ,
2224 cancel : this . cancel ,
@@ -94,7 +96,7 @@ export const createInstance = (defaultProps = {}) => {
9496
9597 render ( ) {
9698 const { children } = this . props
97- if ( typeof children === "function" ) {
99+ if ( isFunction ( children ) ) {
98100 return < Provider value = { this . state } > { children ( this . state ) } </ Provider >
99101 }
100102 if ( children !== undefined && children !== null ) {
@@ -116,7 +118,7 @@ export const createInstance = (defaultProps = {}) => {
116118 if ( state . data !== undefined ) return null
117119 if ( ! persist && state . isLoading ) return null
118120 if ( ! persist && state . error !== undefined ) return null
119- return typeof children === "function" ? children ( state ) : children || null
121+ return isFunction ( children ) ? children ( state ) : children || null
120122 } }
121123 </ Consumer >
122124 )
@@ -132,7 +134,7 @@ export const createInstance = (defaultProps = {}) => {
132134 { state => {
133135 if ( ! state . isLoading ) return null
134136 if ( initial && state . data !== undefined ) return null
135- return typeof children === "function" ? children ( state ) : children || null
137+ return isFunction ( children ) ? children ( state ) : children || null
136138 } }
137139 </ Consumer >
138140 )
@@ -148,7 +150,7 @@ export const createInstance = (defaultProps = {}) => {
148150 { state => {
149151 if ( state . data === undefined ) return null
150152 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
152154 } }
153155 </ Consumer >
154156 )
@@ -164,7 +166,7 @@ export const createInstance = (defaultProps = {}) => {
164166 { state => {
165167 if ( state . error === undefined ) return null
166168 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
168170 } }
169171 </ Consumer >
170172 )
0 commit comments