プロジェクトuse-unmount-signal
概要
A React Hook to cancel promises when a component is unmounted
Web・アプリ開発計測したスター増加
- 24時間
- 0
- 7日間
- 0
- 15日間
- 0
- 1か月
- 履歴を収集中
期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。
README.md
オリジナルuse-unmount-signal

useUnmountSignal is a React Hook to cancel promises when a component is unmounted. It uses the W3C-standard AbortSignal API to notify compatible promises when the calling component is unmounted.
API
useUnmountSignal(): AbortSignal
A React Hook that returns an AbortSignal object that is marked as aborted when the calling component is unmounted.
Example
import useUnmountSignal from 'use-unmount-signal';
function Example() {
// useUnmountSignal returns an AbortSignal object that APIs like fetch accept
const unmountSignal = useUnmountSignal();
return (
<button
onClick={() =>
fetch('https://ping.example.com', { signal: unmountSignal })
}>
Ping
</button>
);
}
With async function event handlers
Any web platform API using promises to represent operations that can be aborted must adhere to the following:
- Accept
AbortSignalobjects through asignaldictionary member.- Convey that the operation got aborted by rejecting the promise with an "
AbortError"DOMException.- Reject immediately if the
AbortSignal's aborted flag is already set, otherwise:- Use the abort algorithms mechanism to observe changes to the
AbortSignalobject and do so in a manner that does not lead to clashes with other observers.
Calling any async function creates a promise. Therefore, authors of async functions need to follow the above guidance to write abortable functions.
import { useState } from 'react';
import useUnmountSignal from 'use-unmount-signal';
function Example() {
const unmountSignal = useUnmountSignal();
const [status, setStatus] = useState('ready');
const handleClick = useCallback(
async () => {
if (unmountSignal.aborted) { throw new AbortError(); }
const response = await fetch('https://ping.example.com', { signal: unmountSignal });
if (unmountSignal.aborted) { throw new AbortError(); }
// We are guaranteed that the component is still mounted at this point
if (response.ok) {
setStatus('OK');
} else {
setStatus(response.status);
}
},
[unmountSignal, setStatus]
);
return <button onClick={handleClick}>Ping {status}</button>;
}
関連プロジェクトを探す
読み込み中…