Skip to content

Commit 071a6b9

Browse files
authored
Unmount abort bugfix (#147)
* v0.2.0 * fixed * adding todo
1 parent dea5890 commit 071a6b9

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ Todos
724724
- [ ] tests for GraphQL hooks `useMutation` + `useQuery`
725725
- [ ] tests for stale `response` see this [PR](https://github.com/alex-cory/use-http/pull/119/files)
726726
- [ ] tests to make sure `response.formData()` and some of the other http `response methods` work properly
727+
- [ ] aborts fetch on unmount
727728
- [ ] take a look at how [react-apollo-hooks](https://github.com/trojanowski/react-apollo-hooks) work. Maybe ad `useSubscription` and `const request = useFetch(); request.subscribe()` or something along those lines
728729
- [ ] make this a github package
729730
- [ ] Make work with React Suspense [current example WIP](https://codesandbox.io/s/7ww5950no0)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-http",
3-
"version": "0.1.99",
3+
"version": "0.2.0",
44
"homepage": "http://use-http.com",
55
"main": "dist/index.js",
66
"license": "MIT",

src/useFetch.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,12 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
147147
req()
148148
}
149149
}
150-
// Cancel any running request when unmounting to avoid updating state after component has unmounted
151-
// This can happen if a request's promise resolves after component unmounts
152-
return request.abort
153150
}, dependencies)
154151

152+
// Cancel any running request when unmounting to avoid updating state after component has unmounted
153+
// This can happen if a request's promise resolves after component unmounts
154+
useEffect(() => request.abort, [])
155+
155156
return Object.assign<UseFetchArrayReturn<TData>, UseFetchObjectReturn<TData>>(
156157
[request, makeResponseProxy(res), loading as boolean, error],
157158
{ request, response: makeResponseProxy(res), ...request },

0 commit comments

Comments
 (0)