Skip to content

Commit 39d38a5

Browse files
committed
Make a clear distinction between 'state' and 'options' by avoiding 'props'.
1 parent f6514b8 commit 39d38a5

File tree

7 files changed

+31
-31
lines changed

7 files changed

+31
-31
lines changed

Diff for: docs/_summary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
- [Interfaces](interfaces.md)
1414
- [Configuration options](options.md)
15-
- [Render props](props.md)
15+
- [State properties](state.md)
1616
- [Helper components](helpers.md)

Diff for: docs/interfaces.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ React Async provides several ways to use it. The classic interface is through th
44
backwards compatible to React v16.3. More recent React applications will be using hooks, of which two are provided:
55
`useAsync` and `useFetch`. Functionally, `<Async>` and `useAsync` are equivalent. `useFetch` is a special type of `useAsync` which is tied to the native `fetch` API.
66

7-
React Async accepts a wide range of [configuration options](options.md) and returns a set of [render props](props.md).
7+
React Async accepts a wide range of [configuration options](options.md) and returns a set of [state props](state.md).
88
The way you use these differs slightly between the `useAsync` and `useFetch` hooks, and the `<Async>` component.
99

1010
## `Async` component
1111

1212
```jsx
13-
<Async {...options}>{props => ...}</Async>
13+
<Async {...options}>{state => ...}</Async>
1414
```
1515

1616
- [`options`](options.md) Configuration options
17-
- [`props`](props.md) Render props object
17+
- [`state`](state.md) State object
1818

1919
> We recommend that you pass the options individually, rather than using JSX [spread attributes]. React Async uses
2020
> [render props] to return its state back to you, so it can be used by other components further down the tree.
@@ -25,24 +25,24 @@ The way you use these differs slightly between the `useAsync` and `useFetch` hoo
2525
## `useAsync` hook
2626

2727
```js
28-
const props = useAsync(options)
28+
const state = useAsync(options)
2929
```
3030

31-
- [`props`](props.md) Render props object
31+
- [`state`](state.md) State object
3232
- [`options`](options.md) Configuration options
3333

34-
> We recommend that you pass `options` as an inline object literal, and that you [destructure] the `props` object to
34+
> We recommend that you pass `options` as an inline object literal, and that you [destructure] the `state` object to
3535
> extract the properties you need, unless you have multiple instances in the same component.
3636
3737
[destructure]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring
3838

3939
## `useFetch` hook
4040

4141
```js
42-
const props = useFetch(resource, init, options)
42+
const state = useFetch(resource, init, options)
4343
```
4444

45-
- [`props`](props.md) Render props object
45+
- [`state`](state.md) State object
4646
- [`resource`][fetch api] The resource you want to fetch
4747
- [`init`][fetch api] Custom request options
4848
- [`options`](options.md) Configuration options
@@ -55,8 +55,8 @@ Besides using the `Async` component directly, you can also create your own insta
5555
with options, e.g. to enable global error handling.
5656

5757
```js
58-
const CustomAsync = createInstance(defaultProps, displayName)
58+
const CustomAsync = createInstance(defaultOptions, displayName)
5959
```
6060

61-
- [`defaultProps`](props.md) Render props object
61+
- [`defaultOptions`](options.md) Default configuration options
6262
- `displayName` Name for this instance, used by React DevTools

Diff for: docs/props.md renamed to docs/state.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Render props
1+
# State properties
22

33
These are returned in an object by `useAsync()` or provided by `<Async>` as render props to the `children` function:
44

Diff for: docs/usage.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const loadPlayer = async ({ playerId }, { signal }) => {
124124
return res.json()
125125
}
126126

127-
// createInstance takes a defaultProps object and a displayName (both optional)
127+
// createInstance takes a defaultOptions object and a displayName (both optional)
128128
const AsyncPlayer = createInstance({ promiseFn: loadPlayer }, "AsyncPlayer")
129129

130130
const MyComponent = () => (

Diff for: packages/react-async/src/Async.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
* createInstance allows you to create instances of Async that are bound to a specific promise.
1616
* A unique instance also uses its own React context for better nesting capability.
1717
*/
18-
export const createInstance = (defaultProps = {}, displayName = "Async") => {
18+
export const createInstance = (defaultOptions = {}, displayName = "Async") => {
1919
const { Consumer, Provider } = React.createContext()
2020

2121
class Async extends React.Component {
@@ -32,8 +32,8 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
3232
this.setError = this.setError.bind(this)
3333

3434
const promise = props.promise
35-
const promiseFn = props.promiseFn || defaultProps.promiseFn
36-
const initialValue = props.initialValue || defaultProps.initialValue
35+
const promiseFn = props.promiseFn || defaultOptions.promiseFn
36+
const initialValue = props.initialValue || defaultOptions.initialValue
3737

3838
this.mounted = false
3939
this.counter = 0
@@ -51,11 +51,11 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
5151
setData: this.setData,
5252
setError: this.setError,
5353
}
54-
this.debugLabel = props.debugLabel || defaultProps.debugLabel
54+
this.debugLabel = props.debugLabel || defaultOptions.debugLabel
5555

5656
const { devToolsDispatcher } = globalScope.__REACT_ASYNC__
57-
const _reducer = props.reducer || defaultProps.reducer
58-
const _dispatcher = props.dispatcher || defaultProps.dispatcher || devToolsDispatcher
57+
const _reducer = props.reducer || defaultOptions.reducer
58+
const _dispatcher = props.dispatcher || defaultOptions.dispatcher || devToolsDispatcher
5959
const reducer = _reducer
6060
? (state, action) => _reducer(state, action, asyncReducer)
6161
: asyncReducer
@@ -73,14 +73,14 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
7373
}
7474

7575
componentDidUpdate(prevProps) {
76-
const { watch, watchFn = defaultProps.watchFn, promise, promiseFn } = this.props
76+
const { watch, watchFn = defaultOptions.watchFn, promise, promiseFn } = this.props
7777
if (watch !== prevProps.watch) {
7878
if (this.counter) this.cancel()
7979
return this.load()
8080
}
8181
if (
8282
watchFn &&
83-
watchFn({ ...defaultProps, ...this.props }, { ...defaultProps, ...prevProps })
83+
watchFn({ ...defaultOptions, ...this.props }, { ...defaultOptions, ...prevProps })
8484
) {
8585
if (this.counter) this.cancel()
8686
return this.load()
@@ -124,24 +124,24 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
124124

125125
load() {
126126
const promise = this.props.promise
127-
const promiseFn = this.props.promiseFn || defaultProps.promiseFn
127+
const promiseFn = this.props.promiseFn || defaultOptions.promiseFn
128128
if (promise) {
129129
this.start(() => promise)
130130
.then(this.onResolve(this.counter))
131131
.catch(this.onReject(this.counter))
132132
} else if (promiseFn) {
133-
const props = { ...defaultProps, ...this.props }
133+
const props = { ...defaultOptions, ...this.props }
134134
this.start(() => promiseFn(props, this.abortController))
135135
.then(this.onResolve(this.counter))
136136
.catch(this.onReject(this.counter))
137137
}
138138
}
139139

140140
run(...args) {
141-
const deferFn = this.props.deferFn || defaultProps.deferFn
141+
const deferFn = this.props.deferFn || defaultOptions.deferFn
142142
if (deferFn) {
143143
this.args = args
144-
const props = { ...defaultProps, ...this.props }
144+
const props = { ...defaultOptions, ...this.props }
145145
return this.start(() => deferFn(args, props, this.abortController)).then(
146146
this.onResolve(this.counter),
147147
this.onReject(this.counter)
@@ -150,7 +150,7 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
150150
}
151151

152152
cancel() {
153-
const onCancel = this.props.onCancel || defaultProps.onCancel
153+
const onCancel = this.props.onCancel || defaultOptions.onCancel
154154
onCancel && onCancel()
155155
this.counter++
156156
this.abortController.abort()
@@ -160,7 +160,7 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
160160
onResolve(counter) {
161161
return data => {
162162
if (this.counter === counter) {
163-
const onResolve = this.props.onResolve || defaultProps.onResolve
163+
const onResolve = this.props.onResolve || defaultOptions.onResolve
164164
this.setData(data, () => onResolve && onResolve(data))
165165
}
166166
return data
@@ -170,7 +170,7 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
170170
onReject(counter) {
171171
return error => {
172172
if (this.counter === counter) {
173-
const onReject = this.props.onReject || defaultProps.onReject
173+
const onReject = this.props.onReject || defaultOptions.onReject
174174
this.setError(error, () => onReject && onReject(error))
175175
}
176176
return error

Diff for: packages/react-async/src/Async.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe("createInstance", () => {
220220
expect(onResolve).toHaveBeenCalledWith("done")
221221
})
222222

223-
test("accepts watchFn from defaultProps and passes the defaultProps along", async () => {
223+
test("accepts watchFn from defaultOptions and passes the defaultOptions along", async () => {
224224
const promiseFn = () => resolveTo("done")
225225
const watchFn = jest.fn()
226226
const CustomAsync = createInstance({ promiseFn, watchFn })
@@ -245,7 +245,7 @@ describe("createInstance", () => {
245245
await findByText("resolved")
246246
})
247247

248-
test("custom instance also passes defaultProps to deferFn", async () => {
248+
test("custom instance also passes defaultOptions to deferFn", async () => {
249249
const deferFn = jest.fn().mockReturnValue(resolveTo())
250250
const CustomAsync = createInstance({ deferFn })
251251

Diff for: packages/react-async/src/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export namespace Async {
166166
}
167167

168168
export function createInstance<T>(
169-
defaultProps?: AsyncProps<T>,
169+
defaultOptions?: AsyncProps<T>,
170170
displayName?: string
171171
): (new () => Async<T>) & {
172172
Initial<T>(props: { children?: InitialChildren<T>; persist?: boolean }): JSX.Element

0 commit comments

Comments
 (0)