0.0.3 • Published 3 years ago

@kodai3/use-mounted-state v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

useMountedState

NOTE!: despite having State in its name this hook does not cause component re-render.
This component designed to be used to avoid state updates on unmounted components.

Lifecycle hook providing ability to check component's mount state.
Returns a function that will return true if component mounted and false otherwise.

Usage

import * as React from "react";
import { useMountedState } from "@kodai3/use-mounted-state";

const Demo = () => {
  const isMounted = useMountedState();

  React.useEffect(() => {
    setTimeout(() => {
      if (isMounted()) {
        // ...
      } else {
        // ...
      }
    }, 1000);
  });
};