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

usestate-memoize

v0.0.6

Published

Memoized version of React useState

Downloads

4

Readme

usestate-memoize

memoize/cache solution for React useState

This package uses React.useState and React.useState for cache solution.

What's the "problem" with React.useState

What happen in a code like this one?

import React, { memo, useState } from 'react';

const MyCounter = memo(() => {
	const [ count, setCount ] = useState(0);

	return <>
		<Button onClick={() => setCount(count+1)}>Increment me!</Button>
		<Button onClick={() => setCount(count-1)}>Decrement me!</Button>
		<span>Value is {count}</span>
	</>
});

const Button = memo(({onClick, children}) => {
	console.log("I'm rendered!");
	return (
		<Button onClick={onClick}>{children}</Button>
	);
});

Each time you click a button, Component is re-rendered, and onClick function references are changed (because you create a new function at each render). While this change of references are pretty insignificant Are hooks slow because of creating functions in render?, when we have multiple components, with many componentDidUpdate or useEffects logics, it would be better not to recreate functions each time, and causing children to re-render

Installation

npm i usestate-memoize

Usage

import React, { memo } from 'react';
import useState from 'usestate-memoize';

const _inc = () => c => c+1;
const _dec = () => c => c-1;
const MyCounter = memo(() => {
	const [ count, setCount, defineCountActions, actions ] = useState(0, {
		increment: _inc,
		decrement: _dec,
	});

	return <>
		<Button onClick={actions.increment}>Increment me!</Button>
		<Button onClick={actions.decrement}>Decrement me!</Button>
		<span>Value is {count}</span>
	</>
});

const Button = memo(({onClick, children}) => {
	console.log("I'm rendered!");
	return (
		<Button onClick={onClick}>{children}</Button>
	);
})

usestate-memoize exposes same api as React.useState. It returns a 4 values array:

[0] State value (same as React.useState)

[1] Function to set state value(same as React.useState)

[2] Function to bind a custom function which will be memoized

[3] Shortcut to access auto-memoized functions defined in useState constructor

const _inc = () => c => c+1;
const _dec = () => c => c-1;
const MyCounter = () => {
    const [ count, setCount, defineCountActions] = useState(0);

	const increment = defineCountActions(_inc);
	const decrement = defineCountActions(_dec);

    return <>
        <Button onClick={increment}>Increment me!</Button>
        <Button onClick={decrement}>Decrement me!</Button>
        <span>Value is {count}</span>
    </>
};

Note that _inc and _dec are defined OUTSIDE of component. If you define these functions inside your functional component, they will be re-created each-time, and you'll loose memoization.

usestate-memoized provides a shortcut, to auto-memoized custom actions, passing them in constructor, and getting back them as fourth array value

const _inc = () => c => c+1;
const _dec = () => c => c-1;
const MyCounter = () => {
    const [ count,,, { increment, decrement } ] = useState(0, {
		increment: _inc,
		decrement: _dec,
	});

    return <>
        <Button onClick={increment}>Increment me!</Button>
        <Button onClick={decrement}>Decrement me!</Button>
        <span>Value is {count}</span>
    </>
};

Please not the double arrow function const _inc = () => c => c+1;. If needed, first function argument is the SyntheticEvent returned by React. Keep in mind that SyntheticEvent are immediately destroyed by React after callback. So because these functions are memoized, the SyntheticEvent event will be lost after first call. To bypass this, we trigger a SyntheticEvent.persist() before memoization. Second function argument is the current state value.

const _setValue = ({target:{value}}) => () => value;
const MyInput = () => {
    const [ value,,, { setValue } ] = useState("", {
		setValue: _setValue,
	});

    return <>
        <input onChange={setValue} value={value} />
    </>
};