0.1.11 • Published 5 years ago

cancelonunmount v0.1.11

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

cancelonunmount

A Reac HOC and HOOK to manage Promise cancellation when the component unmounts.

Installation

npm install cancelonunmount --save

HOC Props

export interface WithCancelOnUnmountProps {
    cancelablePromise(promise: Promise<any>): Promise<any>;
    getIsCanceled(): boolean;
}

Usage

import React, { Component } from "react";
import { WithCancelOnUnmountProps, withCancelOnUnmount } from "cancelonunmount";
type Props = WithCancelOnUnmountProps;
interface Experiment{
    date: string;
    name:string;
}
class ExperimentsLayout extends Component<Props, State>{
    constructor(props:Props){
        super(props);
        this.state={};
    }
    public componentDidMount() {
        this.someAPICall();
    }
    
    private someAPICall = () => {
       /* Uses the `cancelablePromise` from props */
        this.props.cancelablePromise(axios.get("/experiments/get-experiments/"))
            .then((experiments: Experiment[] ) => {
                this.setState({ experiments });
            })
            .catch((err:any)=>{
                /* Checks whether the the promise was canceled */
                if(!err.isCanceled){
                    this.setState({experiments:undefined})
                }
            })
    }

    public render() {
        return this.state.experiments ? <div>{'results ' + this.state.experiments.length}</div> : null;
    }
}

License

MIT

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago