All Projects → expo → use-unmount-signal

expo / use-unmount-signal

Licence: MIT License
A React Hook to cancel promises when a component is unmounted

Programming Languages

typescript
32286 projects
shell
77523 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to use-unmount-signal

react-hook-sticky
react hook sticky
Stars: ✭ 19 (-93.12%)
Mutual labels:  hooks
CJMethodLog
Objective-C 函数日志监听系统,可监听任意类,任意类的任意方法的调用日志。
Stars: ✭ 26 (-90.58%)
Mutual labels:  hooks
use-debugger-hooks
A small package of custom React hooks that are useful for debugging changes in React hook dependencies across renders
Stars: ✭ 44 (-84.06%)
Mutual labels:  hooks
entangle
Global state management tool for react hooks inspired by RecoilJS and Jotai using proxies.
Stars: ✭ 26 (-90.58%)
Mutual labels:  hooks
tacklebox
🎣React UX components for handling common interactions
Stars: ✭ 15 (-94.57%)
Mutual labels:  hooks
react-drag
A drag and drop platform based on sortable.js front-end visualization. 一个基于sortable.js的前端可视化搭建的拖拽平台,ui组件采用antd-mobile.通过umi脚手架构建.技术栈采用dva+hooks+umi+antd-mobile+sortable.js+react-color.
Stars: ✭ 51 (-81.52%)
Mutual labels:  hooks
transition-hook
☄️ An extremely light-weight react transition animation hook which is simpler and easier to use than react-transition-group
Stars: ✭ 250 (-9.42%)
Mutual labels:  hooks
use-saga-reducer
Use redux-saga without redux
Stars: ✭ 72 (-73.91%)
Mutual labels:  hooks
git-templates
Templates / Hooks for Your Git Repositories
Stars: ✭ 30 (-89.13%)
Mutual labels:  hooks
eventrix
Open-source, Predictable, Scaling JavaScript library for state managing and centralizing application global state. State manage system for react apps.
Stars: ✭ 35 (-87.32%)
Mutual labels:  hooks
feathers-shallow-populate
Feathersjs populate relational data
Stars: ✭ 21 (-92.39%)
Mutual labels:  hooks
vscode-react-javascript-snippets
Extension for React/Javascript snippets with search supporting ES7+ and babel features
Stars: ✭ 782 (+183.33%)
Mutual labels:  hooks
wagmi
React Hooks for Ethereum
Stars: ✭ 1,691 (+512.68%)
Mutual labels:  hooks
reactjs-player
基于 react hooks 的 video 播放组件,结构简单,代码简洁,扩展方便。
Stars: ✭ 32 (-88.41%)
Mutual labels:  hooks
useStateMachine
The <1 kb state machine hook for React
Stars: ✭ 2,231 (+708.33%)
Mutual labels:  hooks
use-elapsed-time
React hook to measure elapsed time using requestAnimationFrame
Stars: ✭ 42 (-84.78%)
Mutual labels:  hooks
preact-urql
Preact bindings for urql
Stars: ✭ 28 (-89.86%)
Mutual labels:  hooks
react-typescript-hooks-realworld
conduit realworld application with [ React + Typescript + Redux + Hooks ]
Stars: ✭ 20 (-92.75%)
Mutual labels:  hooks
useAudioPlayer
Custom React hook & context for controlling browser audio
Stars: ✭ 176 (-36.23%)
Mutual labels:  hooks
hooks
A DLL that performs IAT hooking
Stars: ✭ 21 (-92.39%)
Mutual labels:  hooks

use-unmount-signal Tests codecov

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

The HTML5 specification says:

Any web platform API using promises to represent operations that can be aborted must adhere to the following:

  • Accept AbortSignal objects through a signal dictionary 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 AbortSignal object 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');

  async function handleClick({ signal }) {
    if (signal.aborted) { throw new AbortError(); }

    const response = await fetch('https://ping.example.com', { signal });
    if (signal.aborted) { throw new AbortError(); }

    // We are guaranteed that the component is still mounted at this point
    if (response.ok) {
      setState('OK');
    } else {
      setState(response.status);
    }
  }

  return <button onClick={handleClick}>Ping {status}</button>;
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].