Skip to content

Commit 83b8dcd

Browse files
committed
Update README to clarify updates of promiseFn.
1 parent 261b92f commit 83b8dcd

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

README.md

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,19 @@ Similarly, this allows you to set default `onResolve` and `onReject` callbacks.
120120

121121
`<Async>` takes the following properties:
122122

123-
- `promiseFn` {() => Promise} A function that returns a promise; invoked immediately in `componentDidMount` and receives props (object) as argument
124-
- `deferFn` {() => Promise} A function that returns a promise; invoked only by calling `run`, with arguments being passed through
123+
- `promiseFn` {() => Promise} A function that returns a promise; invoked in `componentDidMount` and `componentDidUpdate`; receives props (object) as argument
124+
- `deferFn` {() => Promise} A function that returns a promise; invoked only by calling `run`, with arguments being passed through, as well as props (object) as final argument
125125
- `watch` {any} Watches this property through `componentDidUpdate` and re-runs the `promiseFn` when the value changes (`oldValue !== newValue`)
126126
- `initialValue` {any} initial state for `data` or `error` (if instance of Error); useful for server-side rendering
127127
- `onResolve` {Function} Callback function invoked when a promise resolves, receives data as argument
128128
- `onReject` {Function} Callback function invoked when a promise rejects, receives error as argument
129129

130+
> Be aware that updating `promiseFn` will trigger it to cancel any pending promise and load the new promise. Passing an
131+
> arrow function will cause it to change and reload on every render of the parent component. You can avoid this by
132+
> defining the `promiseFn` value **outside** of the render method. If you need to pass variables to the `promiseFn`,
133+
> pass them as additional props to `<Async>`, as `promiseFn` will be invoked with these props. Alternatively you can
134+
> use [memoization](https://github.com/alexreardon/memoize-one) to avoid unnecessary updates.
135+
130136
### Render props
131137

132138
`<Async>` provides the following render props:
@@ -148,25 +154,34 @@ Similarly, this allows you to set default `onResolve` and `onReject` callbacks.
148154
### Basic data fetching with loading indicator, error state and retry
149155

150156
```js
151-
<Async promiseFn={loadJson}>
152-
{({ data, error, isLoading, reload }) => {
153-
if (isLoading) {
154-
return <div>Loading...</div>
155-
}
156-
if (error) {
157-
return (
158-
<div>
159-
<p>{error.toString()}</p>
160-
<button onClick={reload}>try again</button>
161-
</div>
162-
)
163-
}
164-
if (data) {
165-
return <pre>{JSON.stringify(data, null, 2)}</pre>
166-
}
167-
return null
168-
}}
169-
</Async>
157+
class App extends Component {
158+
getSession = ({ sessionId }) => fetch(...)
159+
160+
render() {
161+
// The promiseFn should be defined outside of render()
162+
return (
163+
<Async promiseFn={this.getSession} sessionId={123}>
164+
{({ data, error, isLoading, reload }) => {
165+
if (isLoading) {
166+
return <div>Loading...</div>
167+
}
168+
if (error) {
169+
return (
170+
<div>
171+
<p>{error.toString()}</p>
172+
<button onClick={reload}>try again</button>
173+
</div>
174+
)
175+
}
176+
if (data) {
177+
return <pre>{JSON.stringify(data, null, 2)}</pre>
178+
}
179+
return null
180+
}}
181+
</Async>
182+
)
183+
}
184+
}
170185
```
171186

172187
### Using `deferFn` to trigger an update (e.g. POST / PUT request)

0 commit comments

Comments
 (0)