Giter VIP home page Giter VIP logo

react-hook-abortable-effect's Introduction

useAbortableEffect

GitHub license npm version

React hook to work with abortable effects in a safe way.

An effect started with useAbortableEffect can be aborted at any time and will be aborted when the React component is unmounted.

While some libraries like axios and Bluebird support aborting effects, and it wouldn't be too complicated to abort then when a component is unmounted, useAbortableEffect does this automatically, which I think is safer, and usign the standard AbortController. This means effects used with useAbortableEffect can be used anywhere: in a redux store, outside of the React lifecycle, etc, without any dependency.

More importantly, with just useEffect you'd likely need some way for your effect error handler to check the mount status of your component in order to prevent setting state or other unintended work when the abort occurs in the context of unmounting the component, which is a bad practice.

I considered accepting a callback to handle the abort scenario, instead of throwing an error, but keeping it like the standard makes the code more portable and durable, at the cost of ergonomics.

Example

This is a TypeScript example.

import React, { FunctionComponent, useState } from "react";

import useAbortableEffect from "./useAbortableEffect";

type DownloadStatus =
  | { status: "Initial" | "InProgress" | "Complete" }
  | { status: "Error"; errorMessage: string };

const assertNever = (unexpected: never): never => {
  throw new Error(`Unexpected value: ${unexpected}`);
};

const LargeVideoDownloader: FunctionComponent<{}> = () => {
  const downloadEffect = useAbortableEffect(signal =>
    fetch("some_url", { signal }), []
  );
  const [downloadStatus, setDownloadStatus] = useState<DownloadStatus>({
    status: "Initial"
  });

  const startDownloading = async () => {
    setDownloadStatus({ status: "InProgress" });
    try {
      await downloadEffect.start();
    } catch (e) {
      const errorMessage = e instanceof Error ? e.message : e.toString();
      // Here we would capture the abort as well as any other error.
      // You can tell the abort case if you need to by checking its name:
      // e.name === 'AbortError'
      setDownloadStatus({ status: "Error", errorMessage });
      return;
    }
    setDownloadStatus({ status: "Complete" });
  };

  switch (downloadStatus.status) {
    case "Initial":
      return (
        <p>
          Ready?<button onClick={startDownloading}>Go!</button>
        </p>
      );
    case "InProgress":
      return (
        <p>
          Downloading... <button onClick={downloadEffect.abort}>Cancel</button>
        </p>
      );
    case "Complete":
      return (
        <p>
          Download complete! :){" "}
          <button onClick={downloadEffect.abort}>Download again because yes</button>
        </p>
      );
    case "Error":
      return <p>Error while downloading: {downloadStatus.errorMessage}. <button onClick={startDownloading}>Retry</button></p>;
    default:
      return assertNever(downloadStatus);
  }
};

export default LargeVideoDownloader;

react-hook-abortable-effect's People

Contributors

kiliancs avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.