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-lru

v0.1.3

Published

Least recently used (LRU) cache algorithm rendering of React roots

Downloads

10,162

Readme

react-lru

Least recently used (LRU) cache algorithm rendering of React roots

For more in depth discussion on why this is necessary, see my medium post.

Installation

To install run

npm intall --save react-lru

Use this library by importing the two main components into your project

import { renderReactCell, MemoizedReactDomContainers } from 'react-lru';

Usage

This library allows you to render React elements into an HTML element that is never removed (or at least controlled by an upper bound and removed when reaching a set number of cached elements).

The most common place where this might be used is in an infinite scrollable table that does not do proper garbage collection of React components, such as Handsontable.

An example renderer might look like:

function sampleRenderer(td, row, col, value) {
    ReactDom.render((<ReactCell
          row={row}
          col={col}
        >{value}</ReactCell>), td);
    return td;
}

This, in Handsontable, will mount React components on almost every render iteration while the user scrolls because React is not able to diff the root node, which changes all the time.

To use react-lru, create an instance of the memoized containers (the least recently used cache) and use the safe rendering function:

const memoizedContainers = new MemoizedReactDomContainers(2000);

function sampleRenderer(td, row, col, value) {
    renderReactCell({
        memoizedContainers,
        td,
        row,
        col,
        jsx: (<ReactCell
          row={row}
          col={col}
        >{value}</ReactCell>),
    });
}

The table will now mount at most one ReactCell for each row and column combination (unless the data changes).

Documentation

This library uses js-lru for its Least Recently Used cache implementation. As such, all of those methods (which are almost identical to that of a JavaScript Map object) can be access through the renderedContainers attribute of your MemoizedReactDomContainers instance, ie.

To get the current size of your cache:

const memoizedContainers = new MemoizedReactDomContainers(2000);
console.log(memoizedContainers.renderedContainers.size) // 0 at first

For a full list of API methods, see the js-lru documentation;

renderReactCell

Passed in the form of an options object:

memoizedContainers: a cache instance. The minimum requirements are that it have a method for getting a memoized cell based on the row and column indeces. This library comes with MemoizedReactDomContainers which uses the LRU cache algorithm for an optimal solution. getContainer: (optional) in the case that the MemoizedReactDomContainers class is not used, this is a function that takes row and col as its arguments and returns the memoized container per the user's own implementation of the cache. td: the HTML element to render the memoized container into. Note that this element will get wiped on every call to renderReactCell and the memoized container will be added as a child using the td.appendChild method. row: integer value of the row index col: integer value of the column index jsx: React JSX to render into a memoized container

MemoizedReactDomContainers

Its only argument is the maximum size of the cache. Defaults to 5000.

SimpleMemoizedReactDomContainers

An alternate version of the MemoizedReactDomContainers where LRU is not used but instead uses a "first in, first out" algorithm. May be used in place of the MemoizedReactDomContainers.