77} catch ( e ) { }
88
99const isFunction = arg => typeof arg === "function"
10+ const renderFn = ( children , ...args ) =>
11+ isFunction ( children ) ? children ( ...args ) : children === undefined ? null : children
1012
1113/**
1214 * 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") => {
186188 */
187189 const Initial = ( { children, persist } ) => (
188190 < 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 ) }
195192 </ Consumer >
196193 )
197194
@@ -206,15 +203,11 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
206203 * Renders only while pending (promise is loading).
207204 *
208205 * @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)
210207 */
211208 const Pending = ( { children, initial } ) => (
212209 < 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 ) }
218211 </ Consumer >
219212 )
220213
@@ -233,12 +226,9 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
233226 */
234227 const Fulfilled = ( { children, persist } ) => (
235228 < 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+ }
242232 </ Consumer >
243233 )
244234
@@ -257,11 +247,9 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
257247 */
258248 const Rejected = ( { children, persist } ) => (
259249 < 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+ }
265253 </ Consumer >
266254 )
267255
@@ -276,15 +264,11 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
276264 * Renders only when promise is fulfilled or rejected.
277265 *
278266 * @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
280268 */
281269 const Settled = ( { children, persist } ) => (
282270 < 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 ) }
288272 </ Consumer >
289273 )
290274
0 commit comments