@@ -89,8 +89,13 @@ const GET_DOGS = gql`
8989` ;
9090
9191const 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+ };
9499
95100 return (
96101 < ul>
@@ -100,35 +105,38 @@ const Dogs = () => {
100105 < / ul>
101106 );
102107};
108+
103109```
104110
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)
107112
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.
110118
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:
121120
122121``` javascript
123122import gql from ' graphql-tag' ;
123+ import React , { Suspense } from ' react' ;
124124import { useQuery } from ' react-apollo-hooks' ;
125125
126- const GET_DOGS = gql ` ...` ;
126+ const GET_DOGS = gql `
127+ {
128+ dogs {
129+ id
130+ breed
131+ }
132+ }
133+ ` ;
127134
128135const 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+ }
132140
133141 return (
134142 < ul>
@@ -138,6 +146,12 @@ const Dogs = () => {
138146 < / ul>
139147 );
140148};
149+
150+ const MyComponent = () => (
151+ < Suspense fallback= {< div> Loading... < / div> }>
152+ < Dogs / >
153+ < / Suspense>
154+ );
141155```
142156
143157## useMutation
@@ -264,15 +278,15 @@ component is not supported. You can use `unstable_SuspenseSSR` provided
264278by this library instead:
265279
266280``` javascript
267- import { unstable_SuspenseSSR } from ' react-apollo-hooks' ;
281+ import { unstable_SuspenseSSR as UnstableSuspenseSSR } from ' react-apollo-hooks' ;
268282
269283function MyComponent () {
270284 return (
271- < unstable_SuspenseSSR fallback= {< Spinner / > }>
285+ < UnstableSuspenseSSR fallback= {< Spinner / > }>
272286 < div>
273287 < ComponentWithGraphqlQuery / >
274288 < / div>
275- < / unstable_SuspenseSSR >
289+ < / UnstableSuspenseSSR >
276290 );
277291}
278292```
0 commit comments