@@ -382,9 +382,14 @@ is set to `"application/json"`.
382
382
- ` data ` Last resolved promise value, maintained when new error arrives.
383
383
- ` error ` Rejected promise reason, cleared when new data arrives.
384
384
- ` initialValue ` The data or error that was provided through the ` initialValue ` prop.
385
- - ` isLoading ` Whether or not a Promise is currently pending.
386
385
- ` startedAt ` When the current/last promise was started.
387
386
- ` finishedAt ` When the last promise was resolved or rejected.
387
+ - ` status ` One of: ` initial ` , ` pending ` , ` fulfilled ` , ` rejected ` .
388
+ - ` isInitial ` true when no promise has ever started, or one started but was cancelled.
389
+ - ` isPending ` true when a promise is currently awaiting settlement. Alias: ` isLoading `
390
+ - ` isFulfilled ` true when the last promise was fulfilled with a value. Alias: ` isResolved `
391
+ - ` isRejected ` true when the last promise was rejected with a reason.
392
+ - ` isSettled ` true when the last promise was fulfilled or rejected (not initial or pending).
388
393
- ` counter ` The number of times a promise was started.
389
394
- ` cancel ` Cancel any pending promise.
390
395
- ` run ` Invokes the ` deferFn ` .
@@ -410,12 +415,6 @@ Rejected promise reason, cleared when new data arrives.
410
415
411
416
The data or error that was originally provided through the ` initialValue ` prop.
412
417
413
- #### ` isLoading `
414
-
415
- > ` boolean `
416
-
417
- ` true ` while a promise is pending, ` false ` otherwise.
418
-
419
418
#### ` startedAt `
420
419
421
420
> ` Date `
@@ -428,6 +427,47 @@ Tracks when the current/last promise was started.
428
427
429
428
Tracks when the last promise was resolved or rejected.
430
429
430
+ #### ` status `
431
+
432
+ > ` string `
433
+
434
+ One of: ` initial ` , ` pending ` , ` fulfilled ` , ` rejected ` .
435
+ These are available for import as ` statusTypes ` .
436
+
437
+ #### ` isInitial `
438
+
439
+ > ` boolean `
440
+
441
+ ` true ` while no promise has started yet, or one was started but cancelled.
442
+
443
+ #### ` isPending `
444
+
445
+ > ` boolean `
446
+
447
+ ` true ` while a promise is pending (loading), ` false ` otherwise.
448
+
449
+ Alias: ` isLoading `
450
+
451
+ #### ` isFulfilled `
452
+
453
+ > ` boolean `
454
+
455
+ ` true ` when the last promise was fulfilled (resolved) with a value.
456
+
457
+ Alias: ` isResolved `
458
+
459
+ #### ` isRejected `
460
+
461
+ > ` boolean `
462
+
463
+ ` true ` when the last promise was rejected with an error.
464
+
465
+ #### ` isSettled `
466
+
467
+ > ` boolean `
468
+
469
+ ` true ` when the last promise was either fulfilled or rejected (i.e. not initial or pending)
470
+
431
471
#### ` counter `
432
472
433
473
> ` number `
@@ -471,10 +511,45 @@ invoked after the state update is completed. Returns the error to enable chainin
471
511
React Async provides several helper components that make your JSX more declarative and less cluttered.
472
512
They don't have to be direct children of ` <Async> ` and you can use the same component several times.
473
513
474
- ### ` <Async.Loading> `
514
+ ### ` <Async.Initial> `
515
+
516
+ Renders only while the deferred promise is still waiting to be run, or you have not provided any promise.
517
+
518
+ #### Props
519
+
520
+ - ` persist ` ` boolean ` Show until we have data, even while loading or when an error occurred. By default it hides as soon as the promise starts loading.
521
+ - ` children ` ` function(state: object): Node | Node ` Render function or React Node.
522
+
523
+ #### Examples
524
+
525
+ ``` js
526
+ < Async deferFn= {deferFn}>
527
+ < Async .Initial >
528
+ < p> This text is only rendered while ` run` has not yet been invoked on ` deferFn` .< / p>
529
+ < / Async .Initial >
530
+ < / Async>
531
+ ```
532
+
533
+ ``` js
534
+ < Async .Initial persist>
535
+ {({ error, isLoading, run }) => (
536
+ < div>
537
+ < p> This text is only rendered while the promise has not resolved yet.< / p>
538
+ < button onClick= {run} disabled= {! isLoading}>
539
+ Run
540
+ < / button>
541
+ {error && < p> {error .message }< / p> }
542
+ < / div>
543
+ )}
544
+ < / Async .Initial >
545
+ ```
546
+
547
+ ### ` <Async.Pending> `
475
548
476
549
This component renders only while the promise is loading (unsettled).
477
550
551
+ Alias: ` <Async.Loading> `
552
+
478
553
#### Props
479
554
480
555
- ` initial ` ` boolean ` Show only on initial load (when ` data ` is ` undefined ` ).
@@ -483,19 +558,21 @@ This component renders only while the promise is loading (unsettled).
483
558
#### Examples
484
559
485
560
``` js
486
- < Async .Loading initial>
561
+ < Async .Pending initial>
487
562
< p> This text is only rendered while performing the initial load.< / p>
488
- < / Async .Loading >
563
+ < / Async .Pending >
489
564
```
490
565
491
566
``` js
492
- < Async .Loading > {({ startedAt }) => ` Loading since ${ startedAt .toISOString ()} ` }< / Async .Loading >
567
+ < Async .Pending > {({ startedAt }) => ` Loading since ${ startedAt .toISOString ()} ` }< / Async .Pending >
493
568
```
494
569
495
- ### ` <Async.Resolved > `
570
+ ### ` <Async.Fulfilled > `
496
571
497
572
This component renders only when the promise is fulfilled with data (` data !== undefined ` ).
498
573
574
+ Alias: ` <Async.Resolved> `
575
+
499
576
#### Props
500
577
501
578
- ` persist ` ` boolean ` Show old data while loading new data. By default it hides as soon as a new promise starts.
@@ -504,11 +581,11 @@ This component renders only when the promise is fulfilled with data (`data !== u
504
581
#### Examples
505
582
506
583
``` js
507
- < Async .Resolved persist> {data => < pre> {JSON .stringify (data)}< / pre> }< / Async .Resolved >
584
+ < Async .Fulfilled persist> {data => < pre> {JSON .stringify (data)}< / pre> }< / Async .Fulfilled >
508
585
```
509
586
510
587
``` js
511
- < Async .Resolved > {({ finishedAt }) => ` Last updated ${ startedAt .toISOString ()} ` }< / Async .Resolved >
588
+ < Async .Fulfilled > {({ finishedAt }) => ` Last updated ${ startedAt .toISOString ()} ` }< / Async .Fulfilled >
512
589
```
513
590
514
591
### ` <Async.Rejected> `
@@ -530,39 +607,15 @@ This component renders only when the promise is rejected.
530
607
< Async .Rejected > {error => ` Unexpected error: ${ error .message } ` }< / Async .Rejected >
531
608
```
532
609
533
- ### ` <Async.Pending > `
610
+ ### ` <Async.Settled > `
534
611
535
- Renders only while the deferred promise is still pending (not yet run), or you have not provided any promise .
612
+ This component renders only when the promise is fulfilled or rejected .
536
613
537
614
#### Props
538
615
539
- - ` persist ` ` boolean ` Show until we have data, even while loading or when an error occurred . By default it hides as soon as the promise starts loading .
616
+ - ` persist ` ` boolean ` Show old data or error while loading new data . By default it hides as soon as a new promise starts.
540
617
- ` children ` ` function(state: object): Node | Node ` Render function or React Node.
541
618
542
- #### Examples
543
-
544
- ``` js
545
- < Async deferFn= {deferFn}>
546
- < Async .Pending >
547
- < p> This text is only rendered while ` run` has not yet been invoked on ` deferFn` .< / p>
548
- < / Async .Pending >
549
- < / Async>
550
- ```
551
-
552
- ``` js
553
- < Async .Pending persist>
554
- {({ error, isLoading, run }) => (
555
- < div>
556
- < p> This text is only rendered while the promise has not resolved yet.< / p>
557
- < button onClick= {run} disabled= {! isLoading}>
558
- Run
559
- < / button>
560
- {error && < p> {error .message }< / p> }
561
- < / div>
562
- )}
563
- < / Async .Pending >
564
- ```
565
-
566
619
## Usage examples
567
620
568
621
Here's several examples to give you an idea of what's possible with React Async. For fully working examples, please
0 commit comments