CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-addons-shallow-compare

Legacy React addon for performing shallow comparison of props and state to optimize component rendering performance

Overall
score

99%

Overview
Eval results
Files

task.mdevals/scenario-5/

Component Update Optimizer

Build a utility that helps determine whether a React component should update based on changes to its props and state, with special handling for edge cases involving null and undefined values.

Requirements

Create a function that checks if a React component should re-render by comparing its current and next props and state. The function must correctly handle edge cases where props or state may contain null or undefined values without throwing errors.

Core Functionality

Implement a comparison utility that:

  • Accepts a component instance along with the next props and next state
  • Returns a boolean indicating whether the component should update
  • Handles cases where props or state contain null values
  • Handles cases where props or state contain undefined values
  • Works correctly when entire props or state objects are null or undefined
  • Does not throw errors for any combination of null/undefined values

Expected Behavior

The utility should return true when the component should update (differences detected) and false when it should not update (no differences detected). It must work reliably even when:

  • Individual property values are null or undefined
  • The entire props or state objects are null or undefined
  • Comparing between null and undefined values
  • Comparing objects with missing properties versus properties explicitly set to undefined

Test Cases

  • When current props are {a: 1, b: null} and next props are {a: 1, b: null}, the function returns false (no update needed) @test
  • When current props are {a: null} and next props are {a: undefined}, the function returns true (update needed) @test
  • When current state is {count: 5, data: undefined} and next state is {count: 5, data: undefined}, the function returns false (no update needed) @test
  • When current props contain nested null values {user: {name: null}} and next props have the same structure, shallow comparison returns false (no update needed) @test

Implementation

@generates

API

/**
 * Determines if a React component should update based on props and state comparison.
 * Handles null and undefined values correctly without throwing errors.
 *
 * @param {Object} instance - The React component instance with current props and state
 * @param {Object} nextProps - The next props object
 * @param {Object} nextState - The next state object
 * @returns {boolean} true if component should update, false otherwise
 */
function shouldComponentUpdate(instance, nextProps, nextState) {
  // IMPLEMENTATION HERE
}

module.exports = {
  shouldComponentUpdate
};

Dependencies { .dependencies }

react-addons-shallow-compare { .dependency }

Provides shallow comparison functionality for optimizing React component rendering performance.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-react-addons-shallow-compare

tile.json