Skip to content

Commit 625117a

Browse files
committed
Fix ambiguous import issue by renaming the standalone helper components. Improved the TS type definitions.
1 parent 253ba7c commit 625117a

File tree

6 files changed

+163
-82
lines changed

6 files changed

+163
-82
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ by passing in the state, or with `<Async>` by using Context. Each of these compo
267267
rendering of its children based on the current state.
268268

269269
```jsx
270-
import { useAsync, Pending, Fulfilled, Rejected } from "react-async"
270+
import { useAsync, IfPending, IfFulfilled, IfRejected } from "react-async"
271271

272272
const loadCustomer = async ({ customerId }, { signal }) => {
273273
// ...
@@ -277,16 +277,16 @@ const MyComponent = () => {
277277
const state = useAsync({ promiseFn: loadCustomer, customerId: 1 })
278278
return (
279279
<>
280-
<Pending state={state}>Loading...</Pending>
281-
<Rejected state={state}>{error => `Something went wrong: ${error.message}`}</Rejected>
282-
<Fulfilled state={state}>
280+
<IfPending state={state}>Loading...</IfPending>
281+
<IfRejected state={state}>{error => `Something went wrong: ${error.message}`}</IfRejected>
282+
<IfFulfilled state={state}>
283283
{data => (
284284
<div>
285285
<strong>Loaded some data:</strong>
286286
<pre>{JSON.stringify(data, null, 2)}</pre>
287287
</div>
288288
)}
289-
</Fulfilled>
289+
</IfFulfilled>
290290
</>
291291
)
292292
}
@@ -607,7 +607,7 @@ invoked after the state update is completed. Returns the error to enable chainin
607607
React Async provides several helper components that make your JSX more declarative and less cluttered.
608608
They don't have to be direct children of `<Async>` and you can use the same component several times.
609609

610-
### `<Initial>` / `<Async.Initial>`
610+
### `<IfInitial>` / `<Async.Initial>`
611611

612612
Renders only while the deferred promise is still waiting to be run, or you have not provided any promise.
613613

@@ -622,9 +622,9 @@ Renders only while the deferred promise is still waiting to be run, or you have
622622
```jsx
623623
const state = useAsync(...)
624624
return (
625-
<Initial state={state}>
625+
<IfInitial state={state}>
626626
<p>This text is only rendered while `run` has not yet been invoked on `deferFn`.</p>
627-
</Initial>
627+
</IfInitial>
628628
)
629629
```
630630

@@ -650,7 +650,7 @@ return (
650650
</Async.Initial>
651651
```
652652

653-
### `<Pending>` / `<Async.Pending>`
653+
### `<IfPending>` / `<Async.Pending>`
654654

655655
This component renders only while the promise is pending (loading / unsettled).
656656

@@ -667,9 +667,9 @@ Alias: `<Async.Loading>`
667667
```jsx
668668
const state = useAsync(...)
669669
return (
670-
<Pending state={state}>
670+
<IfPending state={state}>
671671
<p>This text is only rendered while performing the initial load.</p>
672-
</Pending>
672+
</IfPending>
673673
)
674674
```
675675

@@ -683,7 +683,7 @@ return (
683683
<Async.Pending>{({ startedAt }) => `Loading since ${startedAt.toISOString()}`}</Async.Pending>
684684
```
685685

686-
### `<Fulfilled>` / `<Async.Fulfilled>`
686+
### `<IfFulfilled>` / `<Async.Fulfilled>`
687687

688688
This component renders only when the promise is fulfilled (resolved to a value, could be `undefined`).
689689

@@ -700,9 +700,9 @@ Alias: `<Async.Resolved>`
700700
```jsx
701701
const state = useAsync(...)
702702
return (
703-
<Fulfilled state={state}>
703+
<IfFulfilled state={state}>
704704
{data => <pre>{JSON.stringify(data)}</pre>}
705-
</Fulfilled>
705+
</IfFulfilled>
706706
)
707707
```
708708

@@ -716,7 +716,7 @@ return (
716716
</Async.Fulfilled>
717717
```
718718

719-
### `<Rejected>` / `<Async.Rejected>`
719+
### `<IfRejected>` / `<Async.Rejected>`
720720

721721
This component renders only when the promise is rejected.
722722

@@ -730,7 +730,7 @@ This component renders only when the promise is rejected.
730730

731731
```jsx
732732
const state = useAsync(...)
733-
return <Rejected state={state}>Oops.</Rejected>
733+
return <IfRejected state={state}>Oops.</IfRejected>
734734
```
735735

736736
```jsx
@@ -741,7 +741,7 @@ return <Rejected state={state}>Oops.</Rejected>
741741
<Async.Rejected>{error => `Unexpected error: ${error.message}`}</Async.Rejected>
742742
```
743743

744-
### `<Settled>` / `<Async.Settled>`
744+
### `<IfSettled>` / `<Async.Settled>`
745745

746746
This component renders only when the promise is fulfilled or rejected.
747747

@@ -755,7 +755,7 @@ This component renders only when the promise is fulfilled or rejected.
755755

756756
```jsx
757757
const state = useAsync(...)
758-
return <Settled state={state}>{state => `Finished at ${state.finishedAt.toISOString()}`</Settled>
758+
return <IfSettled state={state}>{state => `Finished at ${state.finishedAt.toISOString()}`</IfSettled>
759759
```
760760
761761
## Usage examples

examples/with-typescript/src/App.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
import React, { Component } from "react"
2-
import Async, { createInstance } from "react-async"
2+
import Async, { createInstance, useAsync, IfPending, IfRejected, IfFulfilled } from "react-async"
33
import DevTools from "react-async-devtools"
44
import "./App.css"
55

66
const promiseFn = () => Promise.resolve("baz")
77
const CustomAsync = createInstance({ promiseFn })
88

9+
const UseAsync = () => {
10+
const state = useAsync({ promiseFn })
11+
return (
12+
<>
13+
<IfPending state={state}>Loading...</IfPending>
14+
<IfRejected state={state}>{error => `Something went wrong: ${error.message}`}</IfRejected>
15+
<IfFulfilled state={state}>
16+
{data => (
17+
<div>
18+
<strong>Loaded some data:</strong>
19+
<pre>{JSON.stringify(data, null, 2)}</pre>
20+
</div>
21+
)}
22+
</IfFulfilled>
23+
</>
24+
)
25+
}
26+
927
class App extends Component {
1028
render() {
1129
return (
@@ -19,6 +37,7 @@ class App extends Component {
1937
<CustomAsync>
2038
<CustomAsync.Resolved>{data => <>{data}</>}</CustomAsync.Resolved>
2139
</CustomAsync>
40+
<UseAsync />
2241
</header>
2342
</div>
2443
)

packages/react-async/src/Async.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react"
22

33
import globalScope from "./globalScope"
4-
import { Initial, Pending, Fulfilled, Rejected, Settled } from "./helpers"
4+
import { IfInitial, IfPending, IfFulfilled, IfRejected, IfSettled } from "./helpers"
55
import propTypes from "./propTypes"
66
import { actionTypes, init, dispatchMiddleware, reducer as asyncReducer } from "./reducer"
77

@@ -201,11 +201,11 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
201201

202202
if (propTypes) Async.propTypes = propTypes.Async
203203

204-
const AsyncInitial = props => <Consumer>{st => <Initial {...props} state={st} />}</Consumer>
205-
const AsyncPending = props => <Consumer>{st => <Pending {...props} state={st} />}</Consumer>
206-
const AsyncFulfilled = props => <Consumer>{st => <Fulfilled {...props} state={st} />}</Consumer>
207-
const AsyncRejected = props => <Consumer>{st => <Rejected {...props} state={st} />}</Consumer>
208-
const AsyncSettled = props => <Consumer>{st => <Settled {...props} state={st} />}</Consumer>
204+
const AsyncInitial = props => <Consumer>{st => <IfInitial {...props} state={st} />}</Consumer>
205+
const AsyncPending = props => <Consumer>{st => <IfPending {...props} state={st} />}</Consumer>
206+
const AsyncFulfilled = props => <Consumer>{st => <IfFulfilled {...props} state={st} />}</Consumer>
207+
const AsyncRejected = props => <Consumer>{st => <IfRejected {...props} state={st} />}</Consumer>
208+
const AsyncSettled = props => <Consumer>{st => <IfSettled {...props} state={st} />}</Consumer>
209209

210210
AsyncInitial.displayName = `${displayName}.Initial`
211211
AsyncPending.displayName = `${displayName}.Pending`

packages/react-async/src/helpers.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const renderFn = (children, ...args) =>
1111
* @prop {Object} state React Async state object
1212
* @prop {boolean} persist Show until we have data, even while pending (loading) or when an error occurred
1313
*/
14-
export const Initial = ({ children, persist, state = {} }) =>
14+
export const IfInitial = ({ children, persist, state = {} }) =>
1515
state.isInitial || (persist && !state.data) ? renderFn(children, state) : null
1616

1717
/**
@@ -21,7 +21,7 @@ export const Initial = ({ children, persist, state = {} }) =>
2121
* @prop {Object} state React Async state object
2222
* @prop {boolean} initial Show only on initial load (data is undefined)
2323
*/
24-
export const Pending = ({ children, initial, state = {} }) =>
24+
export const IfPending = ({ children, initial, state = {} }) =>
2525
state.isPending && (!initial || !state.value) ? renderFn(children, state) : null
2626

2727
/**
@@ -31,7 +31,7 @@ export const Pending = ({ children, initial, state = {} }) =>
3131
* @prop {Object} state React Async state object
3232
* @prop {boolean} persist Show old data while pending (promise is loading)
3333
*/
34-
export const Fulfilled = ({ children, persist, state = {} }) =>
34+
export const IfFulfilled = ({ children, persist, state = {} }) =>
3535
state.isFulfilled || (persist && state.data) ? renderFn(children, state.data, state) : null
3636

3737
/**
@@ -41,7 +41,7 @@ export const Fulfilled = ({ children, persist, state = {} }) =>
4141
* @prop {Object} state React Async state object
4242
* @prop {boolean} persist Show old error while pending (promise is loading)
4343
*/
44-
export const Rejected = ({ children, persist, state = {} }) =>
44+
export const IfRejected = ({ children, persist, state = {} }) =>
4545
state.isRejected || (persist && state.error) ? renderFn(children, state.error, state) : null
4646

4747
/**
@@ -51,13 +51,13 @@ export const Rejected = ({ children, persist, state = {} }) =>
5151
* @prop {Object} state React Async state object
5252
* @prop {boolean} persist Show old data or error while pending (promise is loading)
5353
*/
54-
export const Settled = ({ children, persist, state = {} }) =>
54+
export const IfSettled = ({ children, persist, state = {} }) =>
5555
state.isSettled || (persist && state.value) ? renderFn(children, state) : null
5656

5757
if (propTypes) {
58-
Initial.propTypes = propTypes.Initial
59-
Pending.propTypes = propTypes.Pending
60-
Fulfilled.propTypes = propTypes.Fulfilled
61-
Rejected.propTypes = propTypes.Rejected
62-
Settled.propTypes = propTypes.Settled
58+
IfInitial.propTypes = propTypes.Initial
59+
IfPending.propTypes = propTypes.Pending
60+
IfFulfilled.propTypes = propTypes.Fulfilled
61+
IfRejected.propTypes = propTypes.Rejected
62+
IfSettled.propTypes = propTypes.Settled
6363
}

0 commit comments

Comments
 (0)