Skip to content

Instantly share code, notes, and snippets.

@jwchang0206
Created March 2, 2020 11:04
Show Gist options
  • Save jwchang0206/542f3258fca9a58e19d76602f98f1f68 to your computer and use it in GitHub Desktop.
Save jwchang0206/542f3258fca9a58e19d76602f98f1f68 to your computer and use it in GitHub Desktop.

Revisions

  1. jwchang0206 created this gist Mar 2, 2020.
    24 changes: 24 additions & 0 deletions reactClassCancel.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import React, { Component } from 'react';

    class Foo {
    constructor(props) {
    super(props);

    this.tokens = [];
    }

    componentWillUnmount() {
    // Cancel all the tokens generated within this component
    this.tokens.forEach(token => {
    token.abort();
    });
    }

    makeRequest() {
    const controller = new AbortController();
    this.tokens.push(controller);

    // Use this signal for Fetch
    dispatch('ACTION_REQUEST', { signal: controller.signal });
    }
    }