@@ -21,6 +21,7 @@ assumptions about the shape of your data or the type of request.
21
21
- Automatic re-run using ` watch ` prop
22
22
- Accepts ` onResolve ` and ` onReject ` callbacks
23
23
- Supports optimistic updates using ` setData `
24
+ - Supports server-side rendering through ` initialValue `
24
25
25
26
> Versions 1.x and 2.x of ` react-async ` on npm are from a different project abandoned years ago. The original author was
26
27
> kind enough to transfer ownership so the ` react-async ` package name could be repurposed. The first version of
@@ -119,9 +120,10 @@ Similarly, this allows you to set default `onResolve` and `onReject` callbacks.
119
120
120
121
` <Async> ` takes the following properties:
121
122
122
- - ` promiseFn ` {() => Promise} A function that returns a promise; invoked immediately in ` componentDidMount ` and receives props (object) as arguments
123
+ - ` promiseFn ` {() => Promise} A function that returns a promise; invoked immediately in ` componentDidMount ` and receives props (object) as argument
123
124
- ` deferFn ` {() => Promise} A function that returns a promise; invoked only by calling ` run ` , with arguments being passed through
124
125
- ` watch ` {any} Watches this property through ` componentDidUpdate ` and re-runs the ` promiseFn ` when the value changes (` oldValue !== newValue ` )
126
+ - ` initialValue ` {any} initial state for ` data ` or ` error ` (if instance of Error); useful for server-side rendering
125
127
- ` onResolve ` {Function} Callback function invoked when a promise resolves, receives data as argument
126
128
- ` onReject ` {Function} Callback function invoked when a promise rejects, receives error as argument
127
129
@@ -131,6 +133,7 @@ Similarly, this allows you to set default `onResolve` and `onReject` callbacks.
131
133
132
134
- ` data ` {any} last resolved promise value, maintained when new error arrives
133
135
- ` error ` {Error} rejected promise reason, cleared when new data arrives
136
+ - ` initialValue ` {any} the data or error that was provided through the ` initialValue ` prop
134
137
- ` isLoading ` {boolean} ` true ` while a promise is pending
135
138
- ` startedAt ` {Date} when the current/last promise was started
136
139
- ` finishedAt ` {Date} when the last promise was resolved or rejected
@@ -201,6 +204,36 @@ const updateAttendance = attend => fetch(...).then(() => attend, () => !attend)
201
204
< / Async>
202
205
```
203
206
207
+ ### Server-side rendering using ` initialValue ` (e.g. Next.js)
208
+
209
+ ``` js
210
+ static async getInitialProps () {
211
+ // Resolve the promise server-side
212
+ const sessions = await loadSessions ()
213
+ return { sessions }
214
+ }
215
+
216
+ render () {
217
+ const { sessions } = this .props // injected by getInitialProps
218
+ return (
219
+ < Async promiseFn= {loadSessions} initialValue= {sessions}>
220
+ {({ data, error, isLoading, initialValue }) => { // initialValue is passed along for convenience
221
+ if (isLoading) {
222
+ return < div> Loading... < / div>
223
+ }
224
+ if (error) {
225
+ return < p> {error .toString ()}< / p>
226
+ }
227
+ if (data) {
228
+ return < pre> {JSON .stringify (data, null , 2 )}< / pre>
229
+ }
230
+ return null
231
+ }}
232
+ < / Async>
233
+ )
234
+ }
235
+ ```
236
+
204
237
## Helper components
205
238
206
239
` <Async> ` provides several helper components that make your JSX even more declarative.
0 commit comments