@@ -19,16 +19,16 @@ The first function needs to return a valid `Query` or `NamedQuery` from Grapher.
1919``` js
2020// This is a query
2121const query = createQuery ({
22- users: {
23- emails: 1
24- }
22+ users: {
23+ emails: 1 ,
24+ },
2525});
2626
2727// This is a named query
2828const query = createQuery (' usersWithEmails' , {
29- users: {
30- emails: 1
31- }
29+ users: {
30+ emails: 1 ,
31+ },
3232});
3333```
3434
@@ -100,25 +100,25 @@ import React from 'react';
100100import { withQuery } from ' meteor/cultofcoders:grapher-react' ;
101101
102102const 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+ );
118118};
119119
120120export default withQuery (props => {
121- return getPostLists .clone ();
121+ return getPostLists .clone ();
122122})(PostList);
123123```
124124
@@ -168,29 +168,29 @@ The first example uses the query non-reactively (because that is the default). B
168168``` jsx harmony
169169// ...
170170export default withQuery (
171- props => {
172- return getPostLists .clone ();
173- },
174- { reactive: true }
171+ props => {
172+ return getPostLists .clone ();
173+ },
174+ { reactive: true },
175175)(PostList);
176176```
177177
178178As mentioned above, the props received are passed down to the component we wrap, meaning:
179179
180180``` jsx harmony
181181const PostList = ({ data, something }) => {
182- return < div> Something is true ! < / div> ;
182+ return < div> Something is true ! < / div> ;
183183};
184184
185185const Container = withQuery (
186- props => {
187- return getPostLists .clone ();
188- },
189- { reactive: true }
186+ props => {
187+ return getPostLists .clone ();
188+ },
189+ { reactive: true },
190190)(PostList);
191191
192192export default function () {
193- return < Container something= {true } / > ;
193+ return < Container something= {true } / > ;
194194}
195195```
196196
@@ -204,63 +204,63 @@ import React from 'react';
204204import { withQuery } from ' meteor/cultofcoders:grapher-react' ;
205205
206206const 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+ );
213213};
214214
215215export default withQuery (
216- props => {
217- return getPostLists .clone ();
218- },
219- { reactive: false }
216+ props => {
217+ return getPostLists .clone ();
218+ },
219+ { reactive: false },
220220)(PostList);
221221```
222222
223223If you container wraps a single object, and not a list of objects, you can configure your query like this:
224224
225225``` jsx harmony
226226const UserProfile = ({ data, isLoading, error }) => {
227- return < div> {data .email }< / div> ;
227+ return < div> {data .email }< / div> ;
228228};
229229
230230export 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+ },
237237)(UserProfile);
238238```
239239
240240You 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:
241241
242242``` jsx harmony
243243function ErrorComponent ({ error }) {
244- return < div> {error .reason }< / div> ;
244+ return < div> {error .reason }< / div> ;
245245}
246246
247247function LoadingComponent () {
248- return < div> Please wait... < / div> ;
248+ return < div> Please wait... < / div> ;
249249}
250250
251251const UserProfile = ({ data }) => {
252- return < div> {data .email }< / div> ;
252+ return < div> {data .email }< / div> ;
253253};
254254
255255export 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+ },
264264)(UserProfile);
265265```
266266
@@ -272,23 +272,23 @@ To make things even more simple, you can globally define these rules, and all th
272272import { setDefaults } from ' meteor/cultofcoders:grapher-react' ;
273273
274274setDefaults ({
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,
279279});
280280```
281281
282282You can override the defaults at the ` withQuery ` level, for example you want different ` error ` and ` loading ` components, you can simply do:
283283
284284``` jsx harmony
285285export 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+ },
293293)(UserProfile);
294294```
0 commit comments