Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit 308a756

Browse files
committed
Pass the requestKey down
1 parent 2980470 commit 308a756

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ It is called with one argument, `result`, an object with the following keys:
202202
* `doFetch`: A function that makes the HTTP request. See notes below.
203203
* `url`: The URL that was passed into `<Fetch />`.
204204
* `requestName`: The name of the request (see `requestName` below)
205+
* `requestKey`: The computed [request key](./docs/guides/request-keys.md)
205206

206207
There are three common use cases for the `doFetch` prop:
207208

@@ -224,9 +225,7 @@ is based on the request method that you use.
224225
| POST, PUT, PATCH, DELETE | `true` |
225226

226227
```jsx
227-
<Fetch
228-
url="/books"
229-
lazy>
228+
<Fetch url="/books" lazy>
230229
{({ doFetch }) => {
231230
<button onClick={() => doFetch()}>Fetch books</button>;
232231
}}

src/fetch.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function clearResponseCache() {
1414
export class Fetch extends React.Component {
1515
render() {
1616
const { children, requestName, url } = this.props;
17-
const { fetching, response, data, error } = this.state;
17+
const { fetching, response, data, error, requestKey } = this.state;
1818

1919
if (!children) {
2020
return null;
@@ -26,6 +26,7 @@ export class Fetch extends React.Component {
2626
fetching,
2727
response,
2828
data,
29+
requestKey,
2930
error,
3031
doFetch: this.fetchRenderProp
3132
}) || null
@@ -37,6 +38,10 @@ export class Fetch extends React.Component {
3738
super(props, context);
3839

3940
this.state = {
41+
requestKey: getRequestKey({
42+
...props,
43+
method: props.method.toUpperCase()
44+
}),
4045
requestName: props.requestName,
4146
fetching: false,
4247
response: null,
@@ -145,6 +150,7 @@ export class Fetch extends React.Component {
145150
signal
146151
} = Object.assign({}, this.props, options);
147152

153+
// We need to compute a new key, just in case a new value was passed in `doFetch`.
148154
const requestKey = getRequestKey({
149155
url,
150156
method: method.toUpperCase(),
@@ -210,7 +216,10 @@ export class Fetch extends React.Component {
210216
signal
211217
};
212218

213-
this.setState({ fetching: true });
219+
this.setState({
220+
fetching: true,
221+
requestKey
222+
});
214223
const hittingNetwork = !isRequestInFlight(requestKey) || !dedupe;
215224

216225
if (hittingNetwork) {

test/index.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,23 @@ describe('rendering', () => {
5050
test('passes the right object shape to the render function', () => {
5151
const mockRender = jest.fn().mockReturnValue(null);
5252
const wrapper = shallow(
53-
<Fetch url="/test" requestName="tester" children={mockRender} lazy={true} />
53+
<Fetch
54+
url="/test"
55+
requestName="tester"
56+
children={mockRender}
57+
lazy={true}
58+
/>
5459
);
60+
const requestKey = getRequestKey({
61+
url: '/test',
62+
method: 'GET',
63+
responseType: 'json'
64+
});
5565
expect(mockRender).toHaveBeenCalledTimes(1);
5666
expect(mockRender).toBeCalledWith(
5767
expect.objectContaining({
5868
requestName: 'tester',
69+
requestKey,
5970
url: '/test',
6071
fetching: false,
6172
response: null,

0 commit comments

Comments
 (0)