npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-abort-signal

v1.0.3

Published

🌠 safely cancel `fetch` requests in `useEffect`

Downloads

22

Readme

use-abort-signal

English | 简体中文

NPM version NPM downloads

react 的开发过程中,经常会需要在 useEffect 中进行网络请求,然而当 useEffect 的依赖项改变或者组件卸载时,可能网络请求还未完成,造成意料之外的作用。use-abort-signal 便是用于解决这一问题,安全地取消 useEffect 中的 fetch 请求。

使用方法

useAbortSignal

类型:

export function useAbortSignal(effect: (signal: AbortSignal) => Promise<void>, deps?: DependencyList): void
export function useAbortSignal(effect: (signal: AbortSignal) => Promise<void>, callback: () => void, deps?: DependencyList): void

用法:

import useAbortSignal from "use-abort-signal"
// 或者
import { useAbortSignal } from "use-abort-signal"

useAbortSignal(
    async signal => {
        // 做一些事
        // 将 signal 参数传入你的 fetch 请求
        const response = await fetch("xxx", { signal })
        // 如果依赖项改变或者组件卸载时,fetch 请求还未完成,会自动取消,且下面的代码不会被执行
        // 成功获取 response 后要做的事情
    },
    [/** 依赖项 */]
)

如果你需要在依赖项改变或者组件卸载时,执行某些操作,将回调函数作为第二个参数传入即可:

import useAbortSignal from "use-abort-signal"

useAbortSignal(
    async signal => {
        // 做一些事
        // 将 signal 参数传入你的 fetch 请求
        const response = await fetch("xxx", { signal })
        // 如果依赖项改变或者组件卸载时,fetch 请求还未完成,会自动取消,且下面的代码不会被执行
        // 成功获取 response 后要做的事情
    },
    () => {
        // 依赖项改变或者组件卸载时,被执行
    },
    [/** 依赖项 */]
)

useAbortableFetch

类型:

export function useAbortableFetch(effect: (fetch: typeof window.fetch) => Promise<void>, deps?: DependencyList): void
export function useAbortableFetch(effect: (fetch: typeof window.fetch) => Promise<void>, callback: () => void, deps?: DependencyList): void

用法与 useAbortSignal 基本一致,只不过第一个函数的参数从 abortSignal 变成了 fetch

import { useAbortableFetch } from "use-abort-signal"

useAbortableFetch(
    async fetch => {
        // 无需添加 signal,会自动添加
        const response = await fetch("xxx")
    },
    [/** 依赖项 */]
)

useAbortableFetch(
    async fetch => {
        const response = await fetch("xxx")
    },
    () => {
        // 依赖项改变或者组件卸载时,被执行
    },
    [/** 依赖项 */]
)