Package detail

use-disposable

microsoft693kMIT1.0.4

A StrictMode safe hook that will create disposable instance during render

readme

use-disposable

A hook that creates disposable instances during render phase that works with strict mode.

Problem

With the introduction of stricter strict mode in React 18, factories in hooks like useMemo and useState are called twice but useEffect still disposes once. The component will be mounted and unmounted. This makes it hard to create instances during render time that would be cleaned up by useEffect. Let's look at an example where we try to create a Portal component.

import * as React from "react";
import * as ReactDOM from "react-dom";

function Portal({ children }) {
  const domNode = React.useMemo(() => {
    const newElement = document.createElement("div");
    document.body.appendChild(newElement);
    console.log("create DOM node");
    return newElement;
  }, []);

  React.useEffect(() => {
    console.log("effect");
    return () => {
      console.log("dispose DOM node");
      domNode.remove();
    };
  }, [domNode]);

  console.log("render to portal");
  return ReactDOM.createPortal(children, domNode);
}

Let's look at the console output:

> create DOM node
> render to portal
> create DOM node
> render to portal
> effect
> dispose DOM node
> effect

The result: Nothing is in the portal 🚨🚨 why?

During the first render to portal the changes are not commited to DOM and in the end there is one empty portal DOM node. Only after the second render are the DOM changes commited and our content rendered to the second portal DOM node. However this triggers a simulated mount/unmount and our portal DOM node is disposed 🙃

useDispose

import * as React from "react";
import * as ReactDOM from "react-dom";
import { useDisposable } from "use-disposable";

function Portal({ children }) {
  const domNode = React.useDisposable(() => {
    const newElement = document.createElement("div");
    document.body.appendChild(newElement);
    console.log("create DOM node");
    return [newElement, () => newElement.remove()];
  }, []);

  console.log("render to portal");
  return ReactDOM.createPortal(children, domNode);
}

The resulting console output:

> create DOM node
> render to portal
> render to portal

useDispose makes sure that even in strict mode that in both renders only one instance DOM node is created and used.

useIsStrictMode

This is a hook that uses the React fiber to detect if the StrictMode component is in the tree. It powers useDisposable and is also available from this package as a standalong utility.

Limitations

⚠️ Calling useDispose twice in the same component will lead to unpredictable results, make sure this is only called once per component, we are actively trying to find ways around this limitation.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

1.0.4

  • fix: error when building with module bundlers such as webpack #37

1.0.3

25 October 2024

  • chore: update pipeline #39
  • chore: bump dependencies #38
  • build(deps-dev): bump vite from 5.2.11 to 5.4.6 #36
  • build(deps-dev): bump webpack from 5.76.1 to 5.94.0 #35
  • build(deps-dev): bump braces from 3.0.2 to 3.0.3 #34
  • build(deps-dev): bump ws from 8.11.0 to 8.17.1 #33
  • fix: should not throw in React 19 #32
  • build(deps-dev): bump vite from 3.2.8 to 3.2.10 #30
  • build(deps-dev): bump ip from 1.1.8 to 1.1.9 #29
  • build(deps-dev): bump vite from 3.2.7 to 3.2.8 #27
  • build(deps-dev): bump @babel/traverse from 7.20.5 to 7.23.2 #25
  • build(deps-dev): bump postcss from 8.4.19 to 8.4.31 #24
  • Release 1.0.3 604502d
  • chore: fix pipeline bfa49c2

1.0.2

22 August 2023

  • Fixed nullref in useIsStrictMode when external deps provide prod-mode react when expecting dev-mode #23
  • build(deps-dev): bump word-wrap from 1.2.3 to 1.2.4 #22
  • build(deps): bump tough-cookie from 4.1.2 to 4.1.3 #21
  • build(deps): bump vite from 3.2.5 to 3.2.7 #20
  • build(deps): bump vm2 from 3.9.17 to 3.9.18 #19
  • build(deps): bump vm2 from 3.9.15 to 3.9.17 #18
  • build(deps): bump vm2 from 3.9.13 to 3.9.15 #15
  • build(deps): bump webpack from 5.75.0 to 5.76.1 #14
  • build(deps): bump json5 from 2.2.1 to 2.2.3 #11
  • build(deps): bump cacheable-request from 10.2.3 to 10.2.7 #13
  • build(deps): bump http-cache-semantics from 4.1.0 to 4.1.1 #12
  • Release 1.0.2 dad3d86

1.0.1

21 December 2022

  • fix: Compile to es2019 #10
  • Release 1.0.1 9768e29

1.0.0

19 December 2022

  • chore: CI should check if package-lock changed #9
  • chore: useIsStrictMode should always return false in production #8
  • chore: add monosize #7
  • Release 1.0.0 a9fb110

0.1.0-alpha.0

9 December 2022

  • Update README.md #6
  • chore: Create release pipeline yml #5
  • docs: Initialize README with real docs #4
  • chore: Setup release scripts and changelog with release-it #3
  • feat: useDisposable implementation #2
  • feat: Implement useIsStrictMode #1
  • init repo 02c3853
  • Install jsdom 0461e89
  • Initial commit ab4dbf5