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

@wrap-mutant/react-rendered-array

v0.0.2

Published

Allows to avoid heavy array re-render on each data update

Downloads

3

Readme

Wrap mutant. React. Rendered Array (beta)

NPM Version minzip React Rendered Array Github Issues Github Stars GitHub license Telegram

Object mutation is easy and extremelly fast. But such libraries like react make us to rebuild objects on every their change. It's not a problem on simple and small objects. When your object is a big array, your application become slow. When you are trying to handle complicated deeply nested object, it becomes a brain cancer.

Solution is in wrapping that big or complex objects into Proxy object.

Examples

pravosleva's substring-highlight-sample [demo | repo]


This package is addition of @wrap-mutant/react integration. To understand how actually it works please read the docs of @wrap-mutant/core and @wrap-mutant/react.

This package is separated from @wrap-mutant/react because of different release cycles and build targets.

API V2 explaination

About

This package allows you avoid re-render of huge array of components and that's how you win the performance. It will be useful when you have to render large number of components without pagination, for example, in endless scroll (hello, LinkedIn, you are really slow) or when you render SVG.

What do you win? When we render containers, we have 2 loops:

  • Array rebuilding
  • Array mapping into JSX.Element

So, if @wrap-mutant/react's useWMState makes you avoid the first one, this package is aimed to avoid the second. It means this package allows you to render Arrays always by O(1). This is how you win the performance.

I know API makes you think "not exactly what I want..." and you are right. But as engeneers we have to pay something to win something else. You have to implement small adapter component which prepare props for your target component.

Because of provided by this package Arrays are already wrapped, it would be good to pass { wrap: false } into useWMState's options.

Your skill requirement

This package requires you to understand how does react works in deep. It's not so hard. If I as python developer made it you can do it too.

Pre-requirements

As I told earlier this package continues the idea of @wrap-mutant/react. Be sure you have read the docs.

RenderedArray

Let's begin from minimal working example:

import React, { useContext, useCallback } from "react";
import { useWMState, createMutableContext } from "@wrap-mutant/react";
import { RenderedArray } from "@wrap-mutant/react-rendered-array/array";

type MyModel = {
  // objects stored in your Array
};

type MyComponentProps = {
  model: MyModel;
  update: () => void;
  doSmth: (arg: any) => void;
  // other callbacks
};

const MyComponentCTX = createMutableContext({
  update: () => {},
  doSmth: (arg: any) => {},
});

const MyComponent = (props: MyComponentProps) => {
  return <>{/* Just your component render. Avoid using contexts here */}</>;
};

const MyAdapter = (props: MyModel) => {
  const ctx = useContext(MyComponentCTX); // DO NOT UNPACK IT
  return (
    <MyComponent
      model={props}
      update={() => ctx.update()} // DO NOT UNPACK IT
      doSmth={(arg) => ctx.doSmth(arg)} // DO NOT UNPACK IT
    />
  );
};

const recordFactory = RenderedArray({
  Component: MyAdapter,
  keyFunction: (item: MyModel) => item.key,
});

export const MyContainer = () => {
  // Don't forget to pass `wrap: false`
  const [records, updateRecords] = useWMState(recordFactory, { wrap: false });

  // prettier-ignore
  const update = useCallback(() => {/*whatever you need*/}, [/*deps*/]);

  // prettier-ignore
  const doSmth = useCallback((arg: any) => {/*whatever you need*/}, [/*deps*/]);

  return (
    <>
      <MyComponentCTX.Provider value={{ update, doSmth }}>
        {records.render()}
      </MyComponentCTX.Provider>
    </>
  );
};

This example provides both @wrap-mutant/react's and current package's RenderedArray APIs. But here we will talk about RenderedArray:

  • Required options: object:

    • Component: Required react component to render each Array's element
    • keyFunction: Required function generates key from your Array's element
    • count: Optional number parameter meaning how many wrapper objects will be pre-created. More info at @wrap-mutant/core API V2
  • Returns already wrapped into Proxy object Array. Then you may mutate it as you want. Supported methods:

    • Assingment by index, e.g. myArray[42] = {...something}
    • Delete by index, e.g. delete myArray[42]
    • push, pop, shift, unshift, reverse, splice, sort

RenderedHeap (TODO)

Requires to publish my fork of heap implementation

More info about heap data structure. Actually it means always sorted Array. Very useful in graphics, but can be used everywhere you need always sorted data

Any questions?

Don't be afraid to open this library source code -- it's really small. Also we have Telegram Community