Skip to content

Commit

Permalink
Unmount abort bugfix (#147)
Browse files Browse the repository at this point in the history
* v0.2.0

* fixed

* adding todo
  • Loading branch information
alex-cory authored Nov 19, 2019
1 parent dea5890 commit 071a6b9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ Todos
- [ ] tests for GraphQL hooks `useMutation` + `useQuery`
- [ ] tests for stale `response` see this [PR](https://github.com/alex-cory/use-http/pull/119/files)
- [ ] tests to make sure `response.formData()` and some of the other http `response methods` work properly
- [ ] aborts fetch on unmount
- [ ] 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
- [ ] make this a github package
- [ ] Make work with React Suspense [current example WIP](https://codesandbox.io/s/7ww5950no0)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-http",
"version": "0.1.99",
"version": "0.2.0",
"homepage": "http://use-http.com",
"main": "dist/index.js",
"license": "MIT",
Expand Down
7 changes: 4 additions & 3 deletions src/useFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
req()
}
}
// Cancel any running request when unmounting to avoid updating state after component has unmounted
// This can happen if a request's promise resolves after component unmounts
return request.abort
}, dependencies)

// Cancel any running request when unmounting to avoid updating state after component has unmounted
// This can happen if a request's promise resolves after component unmounts
useEffect(() => request.abort, [])

return Object.assign<UseFetchArrayReturn<TData>, UseFetchObjectReturn<TData>>(
[request, makeResponseProxy(res), loading as boolean, error],
{ request, response: makeResponseProxy(res), ...request },
Expand Down

0 comments on commit 071a6b9

Please sign in to comment.