Skip to content

Commit 36f627b

Browse files
committed
Fix bug when preventCallFetch and depends have conflict
1 parent 24f644e commit 36f627b

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-fetch-hook",
3-
"version": "1.3.4",
3+
"version": "1.3.5",
44
"description": "React fetch hook",
55
"main": "./dist/index.js",
66
"scripts": {

src/__tests__/useFetch.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,29 @@ describe("useFetch", () => {
103103
});
104104
});
105105

106+
it("call with url, options with preventCallFetch and depends", async () => {
107+
fetch.mockResponse(JSON.stringify({ data: "12345" }));
108+
const options = {
109+
headers: {
110+
Accept: "application/json, application/xml, text/plain, text/html, *.*",
111+
"Content-Type": "application/json; charset=utf-8"
112+
}
113+
};
114+
115+
const Component = () => {
116+
const result = useFetch("https://google.com", { ...options, depends: [true], preventCallFetch: true });
117+
return <div>{result.data && result.data.data}</div>;
118+
};
119+
120+
const { container, rerender } = render(<Component />);
121+
122+
await wait(() => {
123+
rerender(<Component />);
124+
125+
expect(fetch.mock.calls.length).toEqual(0);
126+
});
127+
});
128+
106129
it("call with url, options with depends", async () => {
107130
fetch.mockResponse(JSON.stringify({ data: "12345" }));
108131
const options = {

src/useFetch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export function useFetch<T>(
2828
const fetchInstance = formatter => (path, options, specialOptions) => {
2929
const { depends, preventCallFetch, ...otherOptions } = options || {};
3030
const _depends = (specialOptions && specialOptions.depends) || depends;
31-
const _preventCallFetch = _depends
32-
? _depends.reduce((accumulator, currentValue) => accumulator || !currentValue, false)
33-
: preventCallFetch;
31+
const _preventCallFetch =
32+
(_depends && _depends.reduce((accumulator, currentValue) => accumulator || !currentValue, false)) ||
33+
preventCallFetch;
3434
if (_preventCallFetch) {
3535
return Promise.resolve();
3636
}

0 commit comments

Comments
 (0)