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 🙏

© 2026 – Pkg Stats / Ryan Hefner

set-state-compare

v1.0.74

Published

setState for React that compares with the current state and only sets the state if changed.

Readme

set-state-compare

Lightweight helpers for React state updates, shape-style state containers, and value comparison utilities.

Install

npm install set-state-compare

Exports

import {
  anythingDifferent,
  arrayDifferent,
  arrayReferenceDifferent,
  isSimpleObject,
  referenceDifferent,
  simpleObjectDifferent,
  simpleObjectValuesDifferent,
  Shape,
  setState
} from "set-state-compare"

State Helpers

setState

Drop-in helper that only applies state updates when values actually change.

import setState from "set-state-compare"

await setState(this, {count: 1})

Shape

Class-based state container with batched rendering support.

import {Shape} from "set-state-compare"

const shape = new Shape(component)
shape.set({count: 1}, () => {
  // called after render
})

Modes:

  • Shape.setMode("queuedForceUpdate") uses forceUpdate with an after-paint queue.
  • Shape.setMode("setState") uses setState on the component.

ShapeComponent

Class-style component wrapper with hooks-friendly state helpers. setup() runs before each render, so initialization in setup() is re-applied every render. It is also the recommended place to call hook-style helpers like useState/useStates.

import {ShapeComponent, shapeComponent} from "set-state-compare/build/shape-component.js"

class Counter extends ShapeComponent {
  render() {
    this.useState("count", 0)
    return React.createElement("div", null, String(this.state.count))
  }
}

export default shapeComponent(Counter)

cache

Instance-level cache with dependency comparison.

const style = this.cache("style", () => ({padding: 8}), [theme, size])

cacheStatic

Class-level cache shared across instances.

const config = this.cacheStatic("config", () => ({padding: 8}), [theme, size])

useShape

Hook-style shape for function components.

import useShape from "set-state-compare/build/use-shape.js"

function Example(props) {
  const shape = useShape(props)
  shape.useState("count", 0)
  return React.createElement("div", null, String(shape.state.count))
}

useShapeHook

Class-based hooks with ShapeComponent-style lifecycle methods like setup, componentDidMount, and componentWillUnmount.

import useShapeHook, {ShapeHook} from "set-state-compare/build/shape-hook.js"

class MyShapeHookClass extends ShapeHook {
  setup() {
    this.useState("count", 0)
  }
}

function Example(props) {
  const shapeHook = useShapeHook(MyShapeHookClass, props)
  return React.createElement("div", null, String(shapeHook.state.count))
}

Comparison Utilities

  • anythingDifferent deep-compares arrays and simple objects.
  • referenceDifferent uses reference comparison for objects/arrays and Object.is for primitives.
  • arrayReferenceDifferent compares array lengths and each element with referenceDifferent.
  • simpleObjectDifferent and simpleObjectValuesDifferent compare plain objects.
  • arrayDifferent compares arrays by value.
  • isSimpleObject checks for plain objects (ignores React internal objects).

Tests

npm test
npm run typecheck