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.
npm install jsondiffpatch-reactVerified import paths — ran on the pinned version, not inferred.
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.
Always provide a robust `objectHash` function that generates a unique and consistent key for each object in an array. This typically involves using an `id` property or a combination of stable properties, e.g., `(obj) => obj.id || obj._id || obj.name || JSON.stringify(obj)`.
Verify your project's `package.json` for `react` and `react-dom` versions. Install or update them to a compatible version using `npm install react react-dom` or `yarn add react react-dom`.
Use `show={true}` if you want to display the entire merged JSON structure with differences highlighted. Use `show={false}` for a more compact view that focuses solely on the differing elements and their immediate context.Add `import React from 'react';` at the top of your component file.
Always ensure that both `left` and `right` props are provided with valid JSON-serializable data (objects, arrays, strings). If an 'empty' state is intended, provide an empty object `{}` or array `[]` rather than `undefined` or `null` for better component stability.Implement and pass an `objectHash` prop that provides a unique and consistent key for each object in your arrays. This helps `jsondiffpatch` track object identities across changes. For example: `<JsonDiffReact objectHash={(obj) => obj.id || JSON.stringify(obj)} />`.