npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-useunmount

v1.0.2

Published

Utilise state and other data on component unmount with minimal code. Often used as the hooks alternative to componentWillUnmount.

Downloads

9

Readme

react-useunmount

NPM JavaScript Style Guide

The problem

When using a useEffect return function for handling component unmounting, state is not available.

The gif below showcases the problem, and that our custom useUnmount works. If you look at the console on component unmount, our custom hook contains the active state unlike the standard useEffect. You can reproduce this below example your self by following the instructions at the bottom of this page "Full Example".

GIF showing why useUnmount is needed and that it works

What this package does

This package is a super simple and minimal hook for storing an array of dependencies within a ref, making the data available on unmount - which is the effect function passed in as the first parameter.

It accepts a function as the first parameter, which gets called on component unmount, and a dependency array as the second parameter which is stored and passed back to the unmount function. Fundamentally it:

  • Creates a ref to store the required data in
  • Initiates a useEffect for when the dependencies change, which updates the ref
  • Initiates a useEffect for component unmount, which calls your unmount function with the dependency data passed back as the first argument

When might I need this?

If you have state data currently stored within context, state or a reducer and you need it available when the component unmounts.

If you don't need any of the above data on unmount, then you may only need a standard React useEffect return function:

useEffect(() => {
  return () => {
    console.log("This will be called when the component unmounts");
  };
}, []);

Install

npm install --save react-useunmount

or with yarn:

yarn add react-useunmount

The core code of this package is just 20 lines, so you may feel more comfortable copying the file dist/index.js into your own internal libraries. If you are using TypeScript, then you should copy src/index.tsx instead.

Usage

import * as React from "react";

import { useUnmount } from "react-useunmount";

const Example = () => {
  const [state, setState] = useState();

  useUnmount(
    // Destructure argument #1 into the same order as passed dependencies
    ([refState]) => {
      console.log("State now available via ref: ", refState);
    },
    [state]
  );

  return </>;
};

Full Example

Want to see the full working example showcasing how this works, and why a standard useEffect isn't enough? Clone this repository and:

cd example && yarn start

License

MIT © WazzaJB