@@ -19,16 +19,16 @@ The first function needs to return a valid `Query` or `NamedQuery` from Grapher.
19
19
``` js
20
20
// This is a query
21
21
const query = createQuery ({
22
- users: {
23
- emails: 1
24
- }
22
+ users: {
23
+ emails: 1 ,
24
+ },
25
25
});
26
26
27
27
// This is a named query
28
28
const query = createQuery (' usersWithEmails' , {
29
- users: {
30
- emails: 1
31
- }
29
+ users: {
30
+ emails: 1 ,
31
+ },
32
32
});
33
33
```
34
34
@@ -100,25 +100,25 @@ import React from 'react';
100
100
import { withQuery } from ' meteor/cultofcoders:grapher-react' ;
101
101
102
102
const PostList = ({ data, isLoading, error }) => {
103
- if (isLoading) {
104
- return < div> Loading< / div> ;
105
- }
106
-
107
- if (error) {
108
- return < div> {error .reason }< / div> ;
109
- }
110
-
111
- return (
112
- < div>
113
- {data .map (post => (
114
- < li key= {post ._id }> {post .title }< / li>
115
- ))}
116
- < / div>
117
- );
103
+ if (isLoading) {
104
+ return < div> Loading< / div> ;
105
+ }
106
+
107
+ if (error) {
108
+ return < div> {error .reason }< / div> ;
109
+ }
110
+
111
+ return (
112
+ < div>
113
+ {data .map (post => (
114
+ < li key= {post ._id }> {post .title }< / li>
115
+ ))}
116
+ < / div>
117
+ );
118
118
};
119
119
120
120
export default withQuery (props => {
121
- return getPostLists .clone ();
121
+ return getPostLists .clone ();
122
122
})(PostList);
123
123
```
124
124
@@ -168,29 +168,29 @@ The first example uses the query non-reactively (because that is the default). B
168
168
``` jsx harmony
169
169
// ...
170
170
export default withQuery (
171
- props => {
172
- return getPostLists .clone ();
173
- },
174
- { reactive: true }
171
+ props => {
172
+ return getPostLists .clone ();
173
+ },
174
+ { reactive: true },
175
175
)(PostList);
176
176
```
177
177
178
178
As mentioned above, the props received are passed down to the component we wrap, meaning:
179
179
180
180
``` jsx harmony
181
181
const PostList = ({ data, something }) => {
182
- return < div> Something is true ! < / div> ;
182
+ return < div> Something is true ! < / div> ;
183
183
};
184
184
185
185
const Container = withQuery (
186
- props => {
187
- return getPostLists .clone ();
188
- },
189
- { reactive: true }
186
+ props => {
187
+ return getPostLists .clone ();
188
+ },
189
+ { reactive: true },
190
190
)(PostList);
191
191
192
192
export default function () {
193
- return < Container something= {true } / > ;
193
+ return < Container something= {true } / > ;
194
194
}
195
195
```
196
196
@@ -204,63 +204,63 @@ import React from 'react';
204
204
import { withQuery } from ' meteor/cultofcoders:grapher-react' ;
205
205
206
206
const PostList = ({ data, isLoading, error, refetch }) => {
207
- return (
208
- < div>
209
- < a onClick= {refetch}> Reload the data< / a>
210
- {/* Rest of the component */ }
211
- < / div>
212
- );
207
+ return (
208
+ < div>
209
+ < a onClick= {refetch}> Reload the data< / a>
210
+ {/* Rest of the component */ }
211
+ < / div>
212
+ );
213
213
};
214
214
215
215
export default withQuery (
216
- props => {
217
- return getPostLists .clone ();
218
- },
219
- { reactive: false }
216
+ props => {
217
+ return getPostLists .clone ();
218
+ },
219
+ { reactive: false },
220
220
)(PostList);
221
221
```
222
222
223
223
If you container wraps a single object, and not a list of objects, you can configure your query like this:
224
224
225
225
``` jsx harmony
226
226
const UserProfile = ({ data, isLoading, error }) => {
227
- return < div> {data .email }< / div> ;
227
+ return < div> {data .email }< / div> ;
228
228
};
229
229
230
230
export default withQuery (
231
- props => {
232
- return getUserProfile .clone ({ userId: props .userId });
233
- },
234
- {
235
- single: true
236
- }
231
+ props => {
232
+ return getUserProfile .clone ({ userId: props .userId });
233
+ },
234
+ {
235
+ single: true ,
236
+ },
237
237
)(UserProfile);
238
238
```
239
239
240
240
You will find yourself repeating the same code over and over again for when the query is loading or it has an error. For this you can do:
241
241
242
242
``` jsx harmony
243
243
function ErrorComponent ({ error }) {
244
- return < div> {error .reason }< / div> ;
244
+ return < div> {error .reason }< / div> ;
245
245
}
246
246
247
247
function LoadingComponent () {
248
- return < div> Please wait... < / div> ;
248
+ return < div> Please wait... < / div> ;
249
249
}
250
250
251
251
const UserProfile = ({ data }) => {
252
- return < div> {data .email }< / div> ;
252
+ return < div> {data .email }< / div> ;
253
253
};
254
254
255
255
export default withQuery (
256
- props => {
257
- return getUserProfile .clone ({ userId: props .userId });
258
- },
259
- {
260
- single: true ,
261
- errorComponent: ErrorComponent,
262
- loadingComponent: LoadingComponent
263
- }
256
+ props => {
257
+ return getUserProfile .clone ({ userId: props .userId });
258
+ },
259
+ {
260
+ single: true ,
261
+ errorComponent: ErrorComponent,
262
+ loadingComponent: LoadingComponent,
263
+ },
264
264
)(UserProfile);
265
265
```
266
266
@@ -272,23 +272,23 @@ To make things even more simple, you can globally define these rules, and all th
272
272
import { setDefaults } from ' meteor/cultofcoders:grapher-react' ;
273
273
274
274
setDefaults ({
275
- reactive: false , // you can default it to true
276
- single: false , // doesn't make sense to default this to true
277
- errorComponent: ErrorComponent,
278
- loadingComponent: LoadingComponent
275
+ reactive: false , // you can default it to true
276
+ single: false , // doesn't make sense to default this to true
277
+ errorComponent: ErrorComponent,
278
+ loadingComponent: LoadingComponent,
279
279
});
280
280
```
281
281
282
282
You can override the defaults at the ` withQuery ` level, for example you want different ` error ` and ` loading ` components, you can simply do:
283
283
284
284
``` jsx harmony
285
285
export default withQuery (
286
- props => {
287
- return getUserProfile .clone ({ userId: props .userId });
288
- },
289
- {
290
- errorComponent: null ,
291
- loadingComponent: AnotherLoadingComponent
292
- }
286
+ props => {
287
+ return getUserProfile .clone ({ userId: props .userId });
288
+ },
289
+ {
290
+ errorComponent: null ,
291
+ loadingComponent: AnotherLoadingComponent,
292
+ },
293
293
)(UserProfile);
294
294
```
0 commit comments