JSON Diff Patch for React

1.0.8 · active · verified Sun Apr 19

jsondiffpatch-react is a React component designed to visualize differences between two JavaScript objects or arrays, built upon the core `jsondiffpatch` library. The current stable version is 1.0.8, with recent minor updates indicating active maintenance rather than rapid feature development. It originated as a fork of the now-archived `jsondiffpatch-for-react` project. Its key differentiator is providing a readily available React component for rendering detailed, annotated JSON deltas in an HTML format, significantly simplifying the integration of JSON comparison UIs into React applications without needing to implement diff visualization from scratch. It offers configurable options such as showing or hiding unchanged values and enabling a custom `objectHash` function, which is particularly beneficial for optimizing array comparison, especially when dealing with lists of objects that may not have inherent stable unique identifiers.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `JsonDiffReact` component to display differences between two JavaScript objects, including examples of changed data with annotations, identical data with custom tips, and how to hide unchanged values for a more concise diff.

import React, { useState } from 'react';
import JsonDiffReact from 'jsondiffpatch-react';

const App = () => {
  const [leftData] = useState({
    id: 1, 
    name: 'Alice',
    age: 30,
    hobbies: ['reading', 'coding', { type: 'music', genre: 'classical' }],
  });
  const [rightData] = useState({
    id: 1, 
    name: 'Alice',
    age: 31,
    hobbies: ['reading', 'gaming', { type: 'music', genre: 'jazz' }],
    status: 'active'
  });
  const [identicalData] = useState({ id: 2, name: 'Bob', email: 'bob@example.com' });

  return (
    <div style={{ padding: '20px' }}>
      <h1>JSON Diff React Examples</h1>
      
      <h2>Changed Data with Annotations</h2>
      <p>Differences between two complex objects, showing annotations.</p>
      <JsonDiffReact
        left={leftData}
        right={rightData}
        show={true}
        annotated={true}
        tips="No differences detected."
        objectHash={(obj) => obj.id || obj.name || JSON.stringify(obj)}
      />

      <h2>Identical Data (with custom tips)</h2>
      <p>When data is identical, a custom tip message is displayed.</p>
      <JsonDiffReact
        left={identicalData}
        right={identicalData}
        show={true}
        annotated={false}
        tips="The objects are exactly the same!"
      />

      <h2>Hiding Unchanged Values</h2>
      <p>Only the differing parts are displayed, providing a more concise view.</p>
      <JsonDiffReact
        left={{ a: 1, b: 2, c: { d: 4 } }}
        right={{ a: 1, b: 3, c: { d: 4, e: 5 } }}
        show={false}
        annotated={false}
      />
    </div>
  );
};

export default App;

view raw JSON →