You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-21Lines changed: 36 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -120,13 +120,19 @@ Similarly, this allows you to set default `onResolve` and `onReject` callbacks.
120
120
121
121
`<Async>` takes the following properties:
122
122
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
125
125
-`watch` {any} Watches this property through `componentDidUpdate` and re-runs the `promiseFn` when the value changes (`oldValue !== newValue`)
126
126
-`initialValue` {any} initial state for `data` or `error` (if instance of Error); useful for server-side rendering
127
127
-`onResolve` {Function} Callback function invoked when a promise resolves, receives data as argument
128
128
-`onReject` {Function} Callback function invoked when a promise rejects, receives error as argument
129
129
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
+
130
136
### Render props
131
137
132
138
`<Async>` provides the following render props:
@@ -148,25 +154,34 @@ Similarly, this allows you to set default `onResolve` and `onReject` callbacks.
148
154
### Basic data fetching with loading indicator, error state and retry
149
155
150
156
```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
-
returnnull
168
-
}}
169
-
</Async>
157
+
classAppextendsComponent {
158
+
getSession= ({ sessionId }) =>fetch(...)
159
+
160
+
render() {
161
+
// The promiseFn should be defined outside of render()
0 commit comments