@@ -9,21 +9,22 @@ the Medium article
99
1010## Install
1111
12- * ` npm install fetch-suspense --save ` or
12+ * ` npm install fetch-suspense ` or
1313* ` yarn add fetch-suspense `
1414
15- ## Example
15+ ## Examples
1616
17- ``` JavaScript
17+ ### Basic Example
18+
19+ ``` javascript
1820import useFetch from ' fetch-suspense' ;
1921import React , { Suspense } from ' react' ;
2022
2123// This fetching component will be delayed by Suspense until the fetch request
22- // resolves.
23- // The return value of useFetch will be the response of the server.
24+ // resolves. The return value of useFetch will be the response of the server.
2425const MyFetchingComponent = () => {
25- const data = useFetch (' /path/to/api' , { method: ' POST' });
26- return ' The server responded with: ' + data ;
26+ const response = useFetch (' /path/to/api' , { method: ' POST' });
27+ return ' The server responded with: ' + response ;
2728};
2829
2930// The App component wraps the asynchronous fetching component in Suspense.
@@ -38,12 +39,12 @@ const App = () => {
3839};
3940```
4041
41- ## Using a Custom Fetch API
42+ ### Using a Custom Fetch API
4243
4344If you don't want to rely on the global ` fetch ` API, you can create your own
4445` useFetch ` hook by importing the ` createUseFetch ` helper function.
4546
46- ``` JavaScript
47+ ``` javascript
4748import { createUseFetch } from ' fetch-suspense' ;
4849import myFetchApi from ' my-fetch-package' ;
4950import React , { Suspense } from ' react' ;
@@ -54,11 +55,43 @@ import React, { Suspense } from 'react';
5455const useFetch = createUseFetch (myFetchApi);
5556
5657// This fetching component will be delayed by Suspense until the fetch request
58+ // resolves. The return value of useFetch will be the response of the server.
59+ const MyFetchingComponent = () => {
60+ const response = useFetch (' /path/to/api' , { method: ' POST' });
61+ return ' The server responded with: ' + response;
62+ };
63+
64+ // The App component wraps the asynchronous fetching component in Suspense.
65+ // The fallback component (loading text) is displayed until the fetch request
5766// resolves.
58- // The return value of useFetch will be the response of the server.
67+ const App = () => {
68+ return (
69+ < Suspense fallback= " Loading..." >
70+ < MyFetchingComponent / >
71+ < / Suspense>
72+ );
73+ };
74+ ```
75+
76+ ### Including Fetch Metadata
77+
78+ To include fetch metadata with your response, include an ` options ` parameter
79+ with ` metadata: true ` .
80+
81+ ``` javascript
82+ import useFetch from ' fetch-suspense' ;
83+ import React , { Suspense } from ' react' ;
84+
85+ // This fetching component will be delayed by Suspense until the fetch request
86+ // resolves. The return value of useFetch will be the response of the server
87+ // AS WELL AS metadata for the request.
5988const MyFetchingComponent = () => {
60- const data = useFetch (' /path/to/api' , { method: ' POST' });
61- return ' The server responded with: ' + data;
89+ const { contentType , response } = useFetch (
90+ ' /path/to/api' ,
91+ { method: ' POST' },
92+ { metadata: true }, // <--
93+ );
94+ return ` The server responded with ${ contentType} : ${ response} ` ;
6295};
6396
6497// The App component wraps the asynchronous fetching component in Suspense.
@@ -72,3 +105,45 @@ const App = () => {
72105 );
73106};
74107```
108+
109+ ## Options
110+
111+ The supported options for the third, options parameter are:
112+
113+ ### lifespan?: number
114+
115+ _ Default: 0_
116+
117+ The number of milliseconds to cache the result of the request. Each time the
118+ component mounts before this many milliseconds have passed, it will return the
119+ response from the last time this same request was made.
120+
121+ If 0, the cache will be last the remainder of the browser session.
122+
123+ ### metadata?: boolean
124+
125+ _ Default: false_
126+
127+ If true, the ` useFetch ` hook will return metadata _ in addition to_ the response
128+ from the fetch request. Instead of returning just the response, an interface
129+ as follows will be returned:
130+
131+ ``` typescript
132+ interface UseFetchResponse {
133+ bodyUsed: boolean ;
134+ contentType: null | string ;
135+ headers: Headers ;
136+ ok: boolean ;
137+ redirected: boolean ;
138+ // The same response from the server that would be returned if metadata were
139+ // false. It is an Object is the server responded with JSON, and it is a
140+ // string if the server responded with plain text.
141+ response: Object | string ;
142+ status: number ;
143+ statusText: string ;
144+ url: string ;
145+ }
146+ ```
147+
148+ You can access these properties easily through destructuring. See
149+ [ Including Fetch Metadata] ( #including-fetch-metadata ) .
0 commit comments