Skip to content

Commit 427b853

Browse files
committed
Pass props to promiseFn.
1 parent e8c7f04 commit 427b853

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ Creating a custom instance of Async, bound to a specific promiseFn:
9999
```js
100100
import { createInstance } from "react-async"
101101

102-
const loadCustomer = () => fetch("/api/customer").then(...)
102+
const loadCustomer = ({ customerId }) => fetch(`/api/customers/${customerId}`).then(...)
103103

104104
const AsyncCustomer = createInstance({ promiseFn: loadCustomer })
105105

106106
const MyComponent = () => (
107-
<AsyncCustomer>
107+
<AsyncCustomer customerId="123">
108108
<AsyncCustomer.Resolved>{customer => `Hello ${customer.name}`}</AsyncCustomer.Resolved>
109109
</AsyncCustomer>
110110
)
@@ -116,7 +116,7 @@ Similarly, this allows you to set default `onResolve` and `onReject` callbacks.
116116

117117
`<Async>` takes the following properties:
118118

119-
- `promiseFn` {() => Promise} A function that returns a promise; invoked immediately in `componentDidMount` and without arguments
119+
- `promiseFn` {() => Promise} A function that returns a promise; invoked immediately in `componentDidMount` and receives props (object) as arguments
120120
- `deferFn` {() => Promise} A function that returns a promise; invoked only by calling `run`, with arguments being passed through
121121
- `watch` {any} Watches this property through `componentDidUpdate` and re-runs the `promiseFn` when the value changes (`oldValue !== newValue`)
122122
- `onResolve` {Function} Callback function invoked when a promise resolves, receives data as argument

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const createInstance = (defaultProps = {}) => {
4040
if (!promiseFn) return
4141
this.counter++
4242
this.setState({ isLoading: true, startedAt: new Date(), finishedAt: undefined })
43-
return promiseFn().then(this.onResolve(this.counter), this.onReject(this.counter))
43+
return promiseFn(this.props).then(this.onResolve(this.counter), this.onReject(this.counter))
4444
}
4545

4646
run = (...args) => {

src/spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ test("runs promiseFn on mount", () => {
1616
expect(promiseFn).toHaveBeenCalledTimes(1)
1717
})
1818

19+
test("calls promiseFn with props", () => {
20+
const promiseFn = jest.fn().mockReturnValue(Promise.resolve())
21+
render(<Async promiseFn={promiseFn} anotherProp="123" />)
22+
expect(promiseFn).toHaveBeenCalledWith({ promiseFn, anotherProp: "123" })
23+
})
24+
1925
test("passes resolved data to children as render prop", async () => {
2026
const promiseFn = () => resolveTo("done")
2127
const { getByText } = render(<Async promiseFn={promiseFn}>{({ data }) => data || null}</Async>)

0 commit comments

Comments
 (0)