-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a51eb3
commit f4f4b58
Showing
5 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
title: useAbortableEffect | ||
--- | ||
|
||
# useAbortableEffect | ||
|
||
`useEffect` that gives you an [AbortSignal](https://mdn.io/AbortSignal). | ||
|
||
## Usage | ||
|
||
```js | ||
import { useAbortableEffect } from 'foxact/use-use-abortable-effect'; | ||
|
||
function Component() { | ||
useAbortableEffect(signal => { | ||
item.addEventListener('event', () => { | ||
// ... | ||
}, { signal }) | ||
}, [item]); | ||
} | ||
``` | ||
|
||
```js | ||
// before | ||
useEffect(() => { | ||
let isCancelled = false; | ||
someAsyncStuff().then(data => { | ||
if (!isCancelled) { | ||
setData(data); | ||
} | ||
}); | ||
|
||
return () => { | ||
isCancelled = true; | ||
}; | ||
}, [dataKey]); | ||
|
||
// after | ||
useAbortableEffect((signal) => { | ||
someAsyncStuff().then(data => { | ||
if (!signal.aborted) return | ||
setData(data); | ||
}); | ||
}, [dataKey]); | ||
``` | ||
|
||
Note that [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) requires extra configuration in order to check dependency array for third-party hooks: | ||
|
||
```json filename=".eslintrc.json" copy | ||
{ | ||
"rules": { | ||
"react-hooks/exhaustive-deps": [ | ||
"warn", | ||
{ | ||
"additionalHooks": "useAbortableEffect" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
But if you do not want to configure it, `foxact/use-abortable-effect` also provides another named export `useEffect` as an alias of `useAbortableEffect`: | ||
|
||
```diff | ||
- import { useEffect } from 'react'; | ||
+ import { useEffect } from 'foxact/use-abortable-effect'; | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'client-only'; | ||
import { type EffectCallback, useEffect as useEffectFromReact, type DependencyList } from 'react'; | ||
|
||
export const useAbortableEffect = (callback: (signal: AbortSignal) => ReturnType<EffectCallback>, deps: DependencyList) => { | ||
useEffectFromReact(() => { | ||
const controller = new AbortController(); | ||
const signal = controller.signal; | ||
const f = callback(signal); | ||
return () => { | ||
controller.abort(); | ||
f?.(); | ||
}; | ||
}, deps); | ||
}; | ||
export const useEffect = useAbortableEffect; |