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-batch-mount

v2.1.0

Published

This lib use `React.lazy` feature for schedule batch mount large huge components.

Downloads

6

Readme

React Batch Mount

Demo

This library allows to mount components batched so browser had time to print frames. If you want to mount many huge components (mounting take a long time) and you can't use any of the standard pattens (paging, lazy load, virtualization, ets) - you can try to use this library.

batchMount

The main part of the library is HOC batchMount.

import {batchMount} from "react-batch-mount";

const Some = batchMount(props => {
    return <div>Some</div>
});

Internally batchMount wraps the Component in Promise, which is resolved by the scheduler in the queue. You can pass the options to batchMount in the second parameter.

import {batchMount} from "react-batch-mount";

const Some = batchMount(props => {
    return <div>Some</div>
}, {
    fallback: "Loading...",
    noTrackMount: true,
    mode: "append",
});

Option | Type | Description --- | --- | --- fallback | ReactNode | If you pass this parameter batchMount wraps the passed Component in React.Suspense with fallback. If fallback isn't specified you must wrap in React.Suspense manually outside. noTrackMount | true | batchMount adds to the wrapped Component effect by default that invoke mounted() from the context (bellow) after it's mount. If noTrackMount passed batchMount doesn't add effect and you must invoke the function manually when Component mount is complete. mode | "append" or "prepend" | Specify where to add the Component in the mount queue - at the beginning or at the end. append by default.

BatchRenderContext

batchMount uses BatchRenderContext internally. The context has 2 functions:

  • register - register function that need to invoke for start mount of the Component. In batchMount the function is Promise's resolve function
  • mounted - invoke when Component mount is complete. Invocation decreases internal counter and triggers mount of the next batch.

In the default context realization used the global scheduler so BatchMountScheduler is not required. You can override the config (bellow) for component subtree using BatchMountScheduler.

import {batchMount, BatchMountScheduler} from "react-batch-mount";

const Some = batchMount(props => {
    return <div>Some</div>
}, {
    fallback: "Loading...",
});

const App = () => {
    return (
        <BatchMountScheduler budget={1000}>
            {bigArray.map(x => <Some ... />)}
        </BatchMountScheduler>
    )
}

Config

You can config the scheduler by specifying the batch size or the budget in ms per one batch. The global scheduler you can config by setGlobalConfig.

import {batchMount, setGlobalConfig} from "react-batch-mount";

setGlobalConfig({budget: 200});

const Some = batchMount(props => {
    return <div>Some</div>
});

setGlobalConfig takes an object with field:

  • maxBatchSize: number - explicit specify max component count in one batch
  • budget: number - specify time in ms that you want to spend on mount one batch. Size of the next batch dynamically calculate by mount time and size of the previous batch.
  • delay: number - specify delay between batch mounts in milliseconds
  • trace: boolean - enable scheduler writes logs

For BatchMountScheduler the config is passed through props.

import {BatchMountScheduler} from "react-batch-mount";

const App = () => (
    <>
        <BatchMountScheduler maxBatchSize={5}>
            ...
        </BatchMountScheduler>
        <BatchMountScheduler budget={300}>
            ...
        </BatchMountScheduler>
        <BatchMountScheduler budget={1000} maxBatchSize={5} delay={30}>
            ...
        </BatchMountScheduler>
    </>
)

Performance

The library doesn't use component state, which reduce component rerender count. React.lazy creates special component in fiber. This component rerender when the Promise is resolved. It doesn't rerender its ancestors. In the same time, if the ancestors were rerendered when Promise is pending the component doesn't break.

The library controls only component mount and doesn't affect their update in any way. You can use built in React.memo with batchMount for contorl mount and update.

import {memo} from "react";
import {batchMount} from "react-batch-mount";

const Some = memo(batchMount(props => {
    ...
}, /* batchMount options */), /* memo propsAreEquals */);