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

@stainless-code/react-memo

v1.0.0

Published

Elegantly use custom events in React

Downloads

67

Readme

@stainless-code/react-memo

A utility library to simplify and enhance memoization for React functional components using React.memo.

Features

  • Simplify memoization with React.memo.
  • Fine-grained control over dependency-based re-renders.
  • Support for one-time memoization where components never re-render.
  • Fully type-safe with TypeScript.

Installation

npm

npm install @stainless-code/react-memo

yarn

yarn add @stainless-code/react-memo

pnpm

pnpm add @stainless-code/react-memo

bun

bun add @stainless-code/react-memo

Usage

Enhance your React components with precise memoization:

import { withMemo, withMemoOnce } from "@stainless-code/react-memo";
import React from "react";

const MyComponent: React.FC<{ value: number; onClick: () => void }> = ({
  value,
  onClick,
}) => <button onClick={onClick}>{value}</button>;

// Memoize based on specific dependencies
const MemoizedComponent = withMemo(MyComponent, ["value"]);

// Memoize the component to never re-render
const MemoizedOnceComponent = withMemoOnce(MyComponent);

export default function App() {
  return (
    <>
      <MemoizedComponent value={10} onClick={() => console.log("Clicked!")} />
      <MemoizedOnceComponent
        value={20}
        onClick={() => console.log("Clicked again!")}
      />
    </>
  );
}

Typesafety

Example: Type Mismatch (Fails)

If the dependencyProps contain keys that don't exist on the component's props, TypeScript will throw an error:

import { withMemo } from "@stainless-code/react-memo";
import React from "react";

const MyComponent: React.FC<{ value: number; onClick: () => void }> = ({
  value,
  onClick,
}) => <button onClick={onClick}>{value}</button>;

// ❌ TypeScript Error: "nonExistentProp" does not exist on the props of MyComponent.
const MemoizedComponent = withMemo(MyComponent, ["value", "nonExistentProp"]);

export default MemoizedComponent;

Example: Type Match (Succeeds)

If the dependencyProps accurately reflect the keys of the component's props, TypeScript ensures everything works smoothly:

import { withMemo } from "@stainless-code/react-memo";
import React from "react";

const MyComponent: React.FC<{ value: number; onClick: () => void }> = ({
  value,
  onClick,
}) => <button onClick={onClick}>{value}</button>;

// ✅ TypeScript passes: "value" and "onClick" are valid props for MyComponent.
const MemoizedComponent = withMemo(MyComponent, ["value", "onClick"]);

export default MemoizedComponent;

API

withMemo

Enhance a functional component with memoization, allowing re-renders only when specific dependencies or props change.

| Parameter | Type | Default | Description | | ------------------ | ------------- | ----------- | -------------------------------------------------------------------------------- | | Component | React.FC<T> | Required | The React functional component to memoize. | | dependencyProps? | (keyof T)[] | undefined | An array of prop names to check for changes. If omitted, all props are compared. |

Returns a React.MemoExoticComponent that wraps the input component.

withMemoOnce

Memoize a functional component such that it never re-renders, regardless of prop changes.

| Parameter | Type | Default | Description | | ----------- | ------------- | -------- | ------------------------------------------ | | Component | React.FC<T> | Required | The React functional component to memoize. |

Returns a React.MemoExoticComponent that wraps the input component.

Contributing

Feel free to submit issues or pull requests to improve the library. Every bit of help is appreciated. 💖

Read the contribution guidelines.

License

MIT