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-lazy-update

v1.1.0

Published

Optimized update hook for react

Downloads

55

Readme

ci codecov downloads node npm MIT npm bundle size

react-lazy-update

Optimized update hook for react.

Overview

To use state in functional components we can call useState() hook. It returns state and state setter function.

const [ state, setState ] = useState(0);

We can call this function as many times as we need to create separate state variables.

const [ value1, setValue1 ] = useState(0);
const [ value2, setValue2 ] = useState(0);

We can use our new values and update them using setter function:

setValue1(1);

After each state change our component will be re-rendered with new values. But what if we're updating state several times?

const onClick = () => {
    setValue1(1);
    setValue2(2);
};

return (
    <button onClick={ onClick }>update me</button>
);

Fortunatelly React handles such cases - state update will be postponed (called asynchronously) and our component will be re-rendered only once. We can check it by adding some console logs:

const Button = () => {
    console.log('render');

    const [ value1, setValue1 ] = useState(0);
    const [ value2, setValue2 ] = useState(0);

    const onClick = () => {
        console.log('onClick');
        setValue1(1);
        console.log('setValue1');
        setValue2(2);
        console.log('setValue2');
    };

    return (
        <button onClick={ onClick }>update me</button>
    );
}

After click on a button we will see:

onClick
setValue1
setValue2
render

Nice! We updated state twice, but component re-rendered only once.

Unfortunatelly it not always works this way. Let's modify our click handler a bit:

    const onClick = () => {
        console.log('onClick');
        setTimeout(() => {
            console.log('timeout');
            setValue1(1);
            console.log('setValue1');
            setValue2(2);
            console.log('setValue2');
        }, 0);
    };

And then click our button:

onClick
timeout
setValue1
render
setValue2
render

Oops. Now our component renders itself twice. But why? Looks like React have some problems with updating state in some uncontrolled cases (like async callbacks). There is a post about it.

But fortunatelly we can fix it with react-lazy-update!

Installation

As any other npm package react-lazy-update can be added to your project by following command:

npm i -S react-lazy-update

It requires any version of react with new context API support as peer dependency, so it should be installed as well.

npm i -S react

API

lazy

react-lazy-update exports lazy() HoC. We just need to wrap our component with it:

import lazy from 'react-lazy-update';

const Button = () => {
    ...
}

export default lazy(Button);

useState (useLazyState)

For lazy component we just need to replace React.useState() with useLazyState() provided by react-lazy-update:

// import { useState } from 'react';
import lazy, { useLazyState } from 'react-lazy-update';

Or we can use alias useState() to make replacement even easier:

import lazy, { useState } from 'react-lazy-update';

const Button = () => {
    ...
};

export default lazy(Button);

Now our button works as expected and renders only once:

onClick
timeout
setValue1
setValue2
render

Nice again! =)

useReducer (useLazyReducer)

We can also replace React.useReducer() hook with useLazyReducer() provided by react-lazy-update:

import lazy, { useLazyReduccer } from 'react-lazy-update';

const Button = () => {
    const [ value1, dispatch1 ] = useLazyReduccer(reducer1, 0);
    const [ value2, dispatch2 ] = useLazyReduccer(reducer2, 0);

    const onClick = () => {
        setValue1(actionCreator1('click'));
        setValue2(actionCreator2('click'));
    };

    return (
        <button onClick={ onClick }>update me</button>
    );
}

export default lazy(Button);

And, of course, we can use useReducer() alias to make life easier.