@@ -89,8 +89,13 @@ const GET_DOGS = gql`
89
89
` ;
90
90
91
91
const Dogs = () => {
92
- const { data , error } = useQuery (GET_DOGS );
93
- if (error) return ` Error! ${ error .message } ` ;
92
+ const { data , error , loading } = useQuery (GET_DOGS );
93
+ if (loading) {
94
+ return < div> Loading... < / div> ;
95
+ };
96
+ if (error) {
97
+ return ` Error! ${ error .message } ` ;
98
+ };
94
99
95
100
return (
96
101
< ul>
@@ -100,35 +105,38 @@ const Dogs = () => {
100
105
< / ul>
101
106
);
102
107
};
108
+
103
109
```
104
110
105
- To check if data is loaded use the
106
- [ Suspense] ( https://reactjs.org/docs/code-splitting.html#suspense ) component:
111
+ ### Usage with Suspense (experimental)
107
112
108
- ``` javascript
109
- import React , { Suspense } from ' react' ;
113
+ You can use ` useQuery ` with [ React Suspense] ( https://www.youtube.com/watch?v=6g3g0Q_XVb4 )
114
+ with the ` { suspend: true } ` option.
115
+ Please note that it's not yet recommended to use it in production. Please look
116
+ at the [ issue #69 ] ( https://github.com/trojanowski/react-apollo-hooks/issues/69 )
117
+ for details.
110
118
111
- const MyComponent = () => (
112
- < Suspense fallback= {< div> Loading... < / div> }>
113
- < Dogs / >
114
- < / Suspense>
115
- );
116
- ```
117
-
118
- Alternatively you can use the ` useQuery ` hook without suspense with the
119
- ` { suspend: false } ` option. It's required if you want to use non-standard fetch
120
- policy. You have to manage loading state by yourself in that case:
119
+ Example usage:
121
120
122
121
``` javascript
123
122
import gql from ' graphql-tag' ;
123
+ import React , { Suspense } from ' react' ;
124
124
import { useQuery } from ' react-apollo-hooks' ;
125
125
126
- const GET_DOGS = gql ` ...` ;
126
+ const GET_DOGS = gql `
127
+ {
128
+ dogs {
129
+ id
130
+ breed
131
+ }
132
+ }
133
+ ` ;
127
134
128
135
const Dogs = () => {
129
- const { data , error , loading } = useQuery (GET_DOGS , { suspend: false });
130
- if (loading) return < div> Loading... < / div> ;
131
- if (error) return ` Error! ${ error .message } ` ;
136
+ const { data , error } = useQuery (GET_DOGS , { suspend: true });
137
+ if (error) {
138
+ return ` Error! ${ error .message } ` ;
139
+ }
132
140
133
141
return (
134
142
< ul>
@@ -138,6 +146,12 @@ const Dogs = () => {
138
146
< / ul>
139
147
);
140
148
};
149
+
150
+ const MyComponent = () => (
151
+ < Suspense fallback= {< div> Loading... < / div> }>
152
+ < Dogs / >
153
+ < / Suspense>
154
+ );
141
155
```
142
156
143
157
## useMutation
@@ -264,15 +278,15 @@ component is not supported. You can use `unstable_SuspenseSSR` provided
264
278
by this library instead:
265
279
266
280
``` javascript
267
- import { unstable_SuspenseSSR } from ' react-apollo-hooks' ;
281
+ import { unstable_SuspenseSSR as UnstableSuspenseSSR } from ' react-apollo-hooks' ;
268
282
269
283
function MyComponent () {
270
284
return (
271
- < unstable_SuspenseSSR fallback= {< Spinner / > }>
285
+ < UnstableSuspenseSSR fallback= {< Spinner / > }>
272
286
< div>
273
287
< ComponentWithGraphqlQuery / >
274
288
< / div>
275
- < / unstable_SuspenseSSR >
289
+ < / UnstableSuspenseSSR >
276
290
);
277
291
}
278
292
```
0 commit comments