React Shallow Equal Utilities

0.1.1 · abandoned · verified Sun Apr 19

The `react-shallow-equal` package provides specific utility functions for performing efficient shallow equality checks within React and React Native environments. It exposes `propsEqual`, `elementsEqual`, and `stylesEqual` functions, designed primarily to optimize component re-renders through `shouldComponentUpdate` by comparing component properties, React elements, and style objects. The current stable version is 0.1.1. However, the package has not seen updates in approximately 8 years, making its release cadence non-existent. It is a fork of `lelandrichardson/shallow-element-equals` and utilizes concepts from `lelandrichardson/style-equal`. Its key differentiator was its specialized focus on React-specific shallow comparisons, but modern React development largely utilizes `React.PureComponent` and `React.memo` for similar optimizations.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `propsEqual` usage within a React component's `shouldComponentUpdate` to prevent unnecessary re-renders based on shallow property comparison, similar to `PureComponent`.

import React, { PureComponent } from 'react';
import { propsEqual } from 'react-shallow-equal';

class MyOptimizedComponent extends PureComponent {
  // In a traditional React component (not PureComponent), you would implement this:
  // shouldComponentUpdate(nextProps, nextState) {
  //   // Only re-render if props are NOT shallowly equal or state has changed
  //   return !propsEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
  // }

  render() {
    // Simulate rendering based on props, e.g., from an API call or parent component
    const { data, isActive, onAction } = this.props;
    return (
      <div>
        <h2>Item: {data.name}</h2>
        <p>Status: {isActive ? 'Active' : 'Inactive'}</p>
        <button onClick={onAction}>Perform Action</button>
        <p>Last updated: {new Date().toLocaleTimeString()}</p>
      </div>
    );
  }
}

// Example of usage in a parent component
function App() {
  const [count, setCount] = React.useState(0);
  const [itemData, setItemData] = React.useState({ id: 1, name: 'Example Item' });

  const handleAction = React.useCallback(() => {
    console.log('Action performed!');
    setCount(prev => prev + 1);
  }, []);

  // This prop object will be new on every render, but MyOptimizedComponent
  // would only re-render if data.name or isActive actually changed values
  // if using propsEqual inside shouldComponentUpdate.
  // With PureComponent, it handles this automatically with its own shallow comparison.
  const componentProps = {
    data: itemData,
    isActive: count % 2 === 0,
    onAction: handleAction,
    extraProp: count // This will cause PureComponent to re-render every time it changes
  };

  React.useEffect(() => {
    const interval = setInterval(() => {
      // This update will cause App to re-render, creating new componentProps
      // PureComponent will then shallow compare these new props.
      setCount(c => c + 1);
    }, 2000);
    return () => clearInterval(interval);
  }, []);

  return (
    <div>
      <h1>Parent App Render Count: {count}</h1>
      <MyOptimizedComponent {...componentProps} />
    </div>
  );
}

view raw JSON →