GithubHelp home page GithubHelp logo

karranb / use-is-mounted-ref Goto Github PK

View Code? Open in Web Editor NEW

This project forked from helderberto/use-is-mounted-ref

0.0 1.0 0.0 388 KB

๐Ÿ“ฆ useIsMountedRef is a React Hook to check when the component is mounted.

License: MIT License

JavaScript 74.91% TypeScript 25.09%

use-is-mounted-ref's Introduction

๐Ÿ“ฆ use-is-mounted-ref

useIsMountedRef is a React Hook to check when the component is mounted.

build version Bundle Size MIT License downloads


Motivation

  • Avoid memory leaks setting states when component are unmounted;
  • Control when component already mounted;
  • Common error when setting state to unmounted component:
Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.

Usage

To start using the use-is-mounted-ref in your project, first install in your project:

yarn add use-is-mounted-ref or npm install use-is-mounted-ref

Avoid set state when unmounted component:
import { useState, useEffect } from 'react';
import useIsMounted from 'use-is-mounted-ref';

function App() {
  const isMounted = useIsMounted();

  const initialState = {
    loading: false,
    error: false,
    data: [],
  };

  const [state, setState] = useState(initialState);

  useEffect(() => {
    fetch('https://www.reddit.com/.json')
      .then((response) => response.json())
      .then(({ data }) => {
        if (isMounted.current) {
          setState((prevState) => {
            return {
              ...prevState,
              loading: false,
              data,
            };
          });
        }
      })
      .catch((err) => {
        if (isMounted.current) {
          setState((prevState) => {
            return { ...prevState, loading: false, error: true };
          });
        }
      });
  }, []);

  return state.loading ? 'Loading...' : 'Found Data!';
}

export default App;

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Bugs and Sugestions

Report bugs or do suggestions using the issues.

License

MIT License ยฉ helderburato

use-is-mounted-ref's People

Contributors

helderberto avatar

Watchers

 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.