-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(signal): remove passed signal from defautlMapOptionsToKey #83
Changes from 3 commits
d79ed5c
8d56cbd
033115d
0903994
aedde24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -337,6 +337,31 @@ | |||||
}; | ||||||
``` | ||||||
|
||||||
### Abort Signal | ||||||
|
||||||
When you neeed to abort the execution of a `useFetchye` call, you may | ||||||
pass a signal as an option in such way. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```jsx | ||||||
import React, { useEffect } from 'react'; | ||||||
import { useFetchye } from 'fetchye'; | ||||||
|
||||||
const AbortComponent = () => { | ||||||
const controller = new AbortController(); | ||||||
useFetchye('http://example.com/api/books', { signal: controller.signal }); | ||||||
|
||||||
useEffect(() => () => controller.abort(), []); | ||||||
Check warning on line 353 in README.md GitHub Actions / Node 12.x
Check warning on line 353 in README.md GitHub Actions / Node 14.x
Check warning on line 353 in README.md GitHub Actions / Node 16.x
|
||||||
|
||||||
return ( | ||||||
<div> | ||||||
<h1>abortable component</h1> | ||||||
</div> | ||||||
); | ||||||
}; | ||||||
``` | ||||||
Instead of setting up a `useEffect` within the component it's possible to pass a hook to signal using packages such as | ||||||
[use-unmount-signal](https://www.npmjs.com/package/use-unmount-signal/v/1.0.0). | ||||||
|
||||||
### Sequential API Execution | ||||||
|
||||||
Passing the 'isLoading' value from one useFetchye call to the 'defer' field of the next will prevent the second call from being made until the first has loaded. | ||||||
|
@@ -748,7 +773,7 @@ | |||||
**Shape** | ||||||
|
||||||
``` | ||||||
const { isLoading, data, error, run } = useFetchye(key, { defer: Boolean, mapOptionsToKey: options => options, ...fetchOptions }, fetcher); | ||||||
const { isLoading, data, error, run } = useFetchye(key, { signal: {}, defer: Boolean, mapOptionsToKey: options => options, ...fetchOptions }, fetcher); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. signal is a fetch api that would be included as part of
Suggested change
|
||||||
``` | ||||||
|
||||||
**Arguments** | ||||||
|
@@ -765,6 +790,7 @@ | |||||
|--------------------|-------------------------------------------------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||||||
| `mapOptionsToKey` | `(options: Options) => transformedOptions` | `false` | A function that maps options to the key that will become part of the cache key | | ||||||
| `mapKeyToCacheKey` | `(key: String, options: Options) => cacheKey: String` | `false` | A function that maps the key for use as the cacheKey allowing direct control of the cacheKey | | ||||||
| `signal` | `Object` | `false` | Prevents execution of `useFetchye` when desired cancelling DOM request (See [Abort Controller](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)). | | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a
Suggested change
|
||||||
| `defer` | `Boolean` | `false` | Prevents execution of `useFetchye` on each render in favor of using the returned `run` function. *Defaults to `false`* | | ||||||
| `initialData` | `Object` | `false` | Seeds the initial data on first render of `useFetchye` to accomodate server side rendering *Defaults to `undefined`* | | ||||||
| `...restOptions` | `ES6FetchOptions` | `true` | Contains any ES6 Compatible `fetch` option. (See [Fetch Options](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Supplying_request_options)) | | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
export const defaultMapOptionsToKey = (options) => { | ||
const { | ||
signal, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make a unit tests that calls this function with this name and shows that it is removed. One that would fail if this change was reverted |
||
defer, | ||
mapOptionsToKey, | ||
initialData, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im not sure that this should be documented in this mannor, else the readme should document all of the fetch apis.
Maybe this should be framed as an example of aborting an inflight request on unmount to demonstraight that
useFetchye
is just a wrapper around fetch.