react-diff-viewer-continued vs react-diff-view vs react-diff-viewer
React Diff Viewer Libraries
react-diff-viewer-continuedreact-diff-viewreact-diff-viewerSimilar Packages:

React Diff Viewer Libraries

React Diff Viewer Libraries are tools designed to visually compare two pieces of text, code, or other content within a React application. These libraries highlight the differences (or 'diffs') between the two inputs, making it easier for users to identify changes, edits, or discrepancies. They are particularly useful in applications like code review tools, version control systems, or any platform where comparing text content is necessary. These libraries typically provide features like line-by-line comparison, syntax highlighting, and customizable styles to enhance the diffing experience.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
react-diff-viewer-continued582,4652101.08 MB2119 days agoMIT
react-diff-view190,0259851.48 MB117 months agoMIT
react-diff-viewer01,639-826 years agoMIT

Feature Comparison: react-diff-viewer-continued vs react-diff-view vs react-diff-viewer

Diff Display Modes

  • react-diff-viewer-continued:

    react-diff-viewer-continued inherits the display modes from react-diff-viewer and may include additional enhancements. It provides similar flexibility while potentially offering improved performance and features.

  • react-diff-view:

    react-diff-view supports a simple line-by-line diff display. It does not provide advanced modes like side-by-side or inline diffs, making it less versatile for complex comparisons.

  • react-diff-viewer:

    react-diff-viewer offers multiple display modes, including side-by-side and inline views. This flexibility allows users to choose the most suitable format for their comparison needs, enhancing usability.

Syntax Highlighting

  • react-diff-viewer-continued:

    react-diff-viewer-continued also supports syntax highlighting, benefiting from the enhancements made in the original react-diff-viewer. It may offer better performance and customization options for highlighting code.

  • react-diff-view:

    react-diff-view does not include built-in syntax highlighting for code. Users may need to implement custom solutions for highlighting code snippets within the diffs.

  • react-diff-viewer:

    react-diff-viewer includes support for syntax highlighting out of the box, making it more suitable for comparing code files. It uses libraries like react-syntax-highlighter to provide visually appealing highlights for code differences.

Accessibility Features

  • react-diff-viewer-continued:

    react-diff-viewer-continued builds on the accessibility features of react-diff-viewer, potentially offering further improvements and refinements. It is a good choice for applications that prioritize inclusive design.

  • react-diff-view:

    react-diff-view has basic accessibility features but lacks comprehensive support for screen readers and keyboard navigation. It may require additional work to make it fully accessible for all users.

  • react-diff-viewer:

    react-diff-viewer is designed with accessibility in mind, providing better support for screen readers and keyboard navigation. It follows best practices to ensure that all users can interact with the diff content effectively.

Customization Options

  • react-diff-viewer-continued:

    react-diff-viewer-continued retains the customization capabilities of react-diff-viewer while potentially adding new props and features that enhance configurability. It is ideal for developers looking for a more flexible and customizable diff viewing solution.

  • react-diff-view:

    react-diff-view allows for basic customization of styles and classes, but it is limited in terms of configurability. Developers may find it challenging to implement more complex customizations without modifying the source code.

  • react-diff-viewer:

    react-diff-viewer provides extensive customization options, including the ability to style different types of diffs (added, removed, unchanged) and configure various aspects of the component. This makes it much more flexible for developers who need to tailor the appearance and behavior of the diff viewer.

Community and Maintenance

  • react-diff-viewer-continued:

    react-diff-viewer-continued is a fork of react-diff-viewer that aims to provide ongoing maintenance and new features. It is a good option for developers looking for a library that is actively being improved and updated.

  • react-diff-view:

    react-diff-view is a relatively simple and stable library, but it may not have a large community or frequent updates. This could limit the availability of support and resources for developers.

  • react-diff-viewer:

    react-diff-viewer has a larger community and is more actively maintained, which means better support, regular updates, and a wealth of resources for developers. It is a more reliable choice for long-term projects.

Ease of Use: Code Examples

  • react-diff-viewer-continued:

    Enhanced Diff Example in react-diff-viewer-continued

    import React from 'react';
    import DiffViewer from 'react-diff-viewer-continued';
    
    const oldText = `const x = 10;\nconst y = 20;`;
    const newText = `const x = 10;\nconst y = 30; // Changed`;
    
    const App = () => (
      <DiffViewer
        oldValue={oldText}
        newValue={newText}
        splitView={true}
        styles={{ diffViewer: { fontFamily: 'monospace' } }}
      />
    );
    
    export default App;
    
  • react-diff-view:

    Simple Diff Example with react-diff-view

    import React from 'react';
    import { Diff } from 'react-diff-view';
    
    const oldText = `Line 1\nLine 2\nLine 3`;
    const newText = `Line 1\nLine 2 modified\nLine 3`;
    
    const App = () => (
      <Diff
        viewType="unified"
        diffType="modify"
        oldValue={oldText}
        newValue={newText}
      />
    );
    
    export default App;
    
  • react-diff-viewer:

    Diff Example with Syntax Highlighting in react-diff-viewer

    import React from 'react';
    import DiffViewer from 'react-diff-viewer';
    
    const oldCode = `function add(a, b) {\n  return a + b;\n}`;
    const newCode = `function add(a, b) {\n  // Adding a comment\n  return a + b;\n}`;
    
    const App = () => (
      <DiffViewer
        oldValue={oldCode}
        newValue={newCode}
        splitView={true}
        enableSyntaxHighlighting={true}
      />
    );
    
    export default App;
    

How to Choose: react-diff-viewer-continued vs react-diff-view vs react-diff-viewer

  • react-diff-viewer-continued:

    Opt for react-diff-viewer-continued if you are looking for a maintained fork of react-diff-viewer with additional features and improvements. This package is ideal for users who want the latest updates and enhancements while retaining compatibility with the original library.

  • react-diff-view:

    Choose react-diff-view if you need a simple, lightweight solution for displaying text diffs with basic customization options. It is ideal for projects that require quick integration without extensive features.

  • react-diff-viewer:

    Select react-diff-viewer if you want a more feature-rich library that supports side-by-side and inline diff views, syntax highlighting, and better accessibility. It is suitable for applications that need a more polished and interactive diffing experience.

README for react-diff-viewer-continued

React Diff Viewer

All Contributors

npm version npm downloads GitHub license

A simple and beautiful text diff viewer component made with Diff and React.

example image

Inspired by the Github diff viewer, it includes features like split view, inline view, word diff, line highlight and more. It is highly customizable and it supports almost all languages.

Most credit goes to Pranesh Ravi who created the original diff viewer. I've just made a few modifications and updated the dependencies so they work with modern stacks.

Install

yarn add react-diff-viewer-continued

# or

npm i react-diff-viewer-continued

# or

pnpm add react-diff-viewer-continued

Usage

import React, { PureComponent } from 'react';
import ReactDiffViewer from 'react-diff-viewer-continued';

const oldCode = `
const a = 10
const b = 10
const c = () => console.log('foo')

if(a > 10) {
  console.log('bar')
}

console.log('done')
`;
const newCode = `
const a = 10
const boo = 10

if(a === 10) {
  console.log('bar')
}
`;

class Diff extends PureComponent {
  render = () => {
    return (
      <ReactDiffViewer oldValue={oldCode} newValue={newCode} splitView={true} />
    );
  };
}

Props

PropTypeDefaultDescription
oldValuestring | Object''Old value as string (or Object if using DiffMethod.JSON).
newValuestring | Object''New value as string (or Object if using DiffMethod.JSON).
splitViewbooleantrueSwitch between unified and split view.
disableWordDiffbooleanfalseShow and hide word diff in a diff line.
compareMethodDiffMethod | (string, string) => diff.Change[]DiffMethod.CHARSJsDiff text diff method. See Text block diff comparison. Important: For JSON files use DiffMethod.JSON, for YAML files use DiffMethod.YAML - these use optimized structural comparison that is significantly faster than generic text diff for large files.
renderGutter(diffData) => ReactNodeundefinedFunction that can be used to render an extra gutter with various information next to the line number.
hideLineNumbersbooleanfalseShow and hide line numbers.
alwaysShowLinesstring[][]List of lines to always be shown, regardless of diff status. Line numbers are prefixed with L and R for the left and right section of the diff viewer, respectively. For example, L-20 means 20th line in the left pane. extraLinesSurroundingDiff applies to these lines as well.
renderContentfunctionundefinedRender Prop API to render code in the diff viewer. Helpful for syntax highlighting.
onLineNumberClickfunctionundefinedEvent handler for line number click. (lineId: string, event: MouseEvent) => void
highlightLinesstring[][]List of lines to be highlighted. Works together with onLineNumberClick. Line numbers are prefixed with L and R for the left and right section of the diff viewer, respectively. For example, L-20 means 20th line in the left pane. To highlight a range of line numbers, pass the prefixed line number as an array. For example, [L-2, L-3, L-4, L-5] will highlight the lines 2-5 in the left pane.
showDiffOnlybooleantrueShows only the diffed lines and folds the unchanged lines.
extraLinesSurroundingDiffnumber3Number of extra unchanged lines surrounding the diff. Works along with showDiffOnly.
codeFoldMessageRendererfunctionundefinedRender Prop API to render code fold message. (totalFoldedLines: number, leftStartLineNumber: number, rightStartLineNumber: number) => ReactElement
stylesobject{}To override style variables and styles. Learn more about overriding styles.
useDarkThemebooleanfalseTo enable/disable dark theme.
leftTitlestring | ReactElementundefinedColumn title for left section of the diff in split view. This will be used as the only title in inline view.
rightTitlestring | ReactElementundefinedColumn title for right section of the diff in split view. This will be ignored in inline view.
linesOffsetnumber0Number to start count code lines from.
summarystring | ReactElementundefinedText or element to display in the summary bar (e.g., filename).
hideSummarybooleanfalseHide the summary bar (expand/collapse button, change count, summary text).
infiniteLoading{ pageSize: number, containerHeight: string }undefinedEnable virtualization for large diffs. When enabled, only visible rows are rendered. containerHeight sets the scrollable container height (e.g., '500px' or '80vh').
loadingElement() => ReactElementundefinedFunction that returns an element to display while the diff is being computed. Useful with infiniteLoading for large files.
noncestring''Nonce to use for inline styles (for CSP).

Instance Methods

resetCodeBlocks() - Resets the expanded code blocks to its initial state. Returns true on successful reset and false during unsuccessful reset.

Large Diffs and Performance

For large files (thousands of lines), the diff viewer provides several features to maintain good performance:

Virtualization with infiniteLoading

Enable virtualization to only render visible rows:

<ReactDiffViewer
  oldValue={largeOldFile}
  newValue={largeNewFile}
  infiniteLoading={{
    pageSize: 20,
    containerHeight: '80vh'
  }}
  loadingElement={() => (
    <div style={{ padding: '20px', textAlign: 'center' }}>
      Computing diff...
    </div>
  )}
/>

When infiniteLoading is enabled:

  • Only visible rows are rendered (virtualization)
  • Word-level diffs are computed on-demand as lines become visible
  • A loading element can be shown while the initial diff is computed

Use optimized diff methods for structured data

For JSON and YAML files, always use the dedicated diff methods:

// For JSON files - up to 100x faster for large files
<ReactDiffViewer
  oldValue={jsonObject}
  newValue={newJsonObject}
  compareMethod={DiffMethod.JSON}
/>

// For YAML files
<ReactDiffViewer
  oldValue={yamlString}
  newValue={newYamlString}
  compareMethod={DiffMethod.YAML}
/>

See JSON and YAML diffing for more details.

Syntax Highlighting

Syntax highlighting is a bit tricky when combined with diff. Here, React Diff Viewer provides a simple render prop API to handle syntax highlighting. Use renderContent(content: string) => JSX.Element and your favorite syntax highlighting library to achieve this.

An example using Prism JS

// Load Prism CSS
<link
  href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/prism.min.css"
/>

// Load Prism JS
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/prism.min.js"></script>
import React, { PureComponent } from 'react';
import ReactDiffViewer from 'react-diff-viewer-continued';

const oldCode = `
const a = 10
const b = 10
const c = () => console.log('foo')

if(a > 10) {
  console.log('bar')
}

console.log('done')
`;
const newCode = `
const a = 10
const boo = 10

if(a === 10) {
  console.log('bar')
}
`;

class Diff extends PureComponent {
  highlightSyntax = (str) => (
    <pre
      style={{ display: 'inline' }}
      dangerouslySetInnerHTML={{
        __html: Prism.highlight(str, Prism.languages.javascript),
      }}
    />
  );

  render = () => {
    return (
      <ReactDiffViewer
        oldValue={oldCode}
        newValue={newCode}
        splitView={true}
        renderContent={this.highlightSyntax}
      />
    );
  };
}

Text block diff comparison

Different styles of text block diffing are possible by using the enums corresponding to various JsDiff methods (learn more). The supported methods are as follows.

enum DiffMethod {
  CHARS = 'diffChars',
  WORDS = 'diffWords',
  WORDS_WITH_SPACE = 'diffWordsWithSpace',
  LINES = 'diffLines',
  TRIMMED_LINES = 'diffTrimmedLines',
  SENTENCES = 'diffSentences',
  CSS = 'diffCss',
  JSON = 'diffJson',  // Optimized for JSON files
  YAML = 'diffYaml',  // Optimized for YAML files
}

JSON and YAML diffing

For JSON and YAML files, use the dedicated DiffMethod.JSON and DiffMethod.YAML methods. These use an optimized structural comparison algorithm that is significantly faster than generic text diff for large files.

Why use these methods?

Generic text diff algorithms (like CHARS or WORDS) compare files character-by-character using the Myers diff algorithm, which has O(ND) complexity where N is the file size and D is the number of differences. For large JSON/YAML files (thousands of lines), this can take several seconds or even freeze the browser.

The JSON and YAML methods instead:

  1. Parse the data structure
  2. Compare objects/arrays structurally
  3. Only run text diff on the parts that actually differ

This reduces comparison time from seconds to milliseconds for typical configuration files.

import React from 'react';
import ReactDiffViewer, { DiffMethod } from 'react-diff-viewer-continued';

// For JSON - can pass objects directly
const oldJson = { name: "Original", items: [1, 2, 3] };
const newJson = { name: "Updated", items: [1, 2, 3, 4] };

// For YAML - pass as strings
const oldYaml = `
name: Original
items:
  - 1
  - 2
`;
const newYaml = `
name: Updated
items:
  - 1
  - 2
  - 3
`;

function JsonDiff() {
  return (
    <ReactDiffViewer
      oldValue={oldJson}
      newValue={newJson}
      compareMethod={DiffMethod.JSON}
      splitView={true}
    />
  );
}

function YamlDiff() {
  return (
    <ReactDiffViewer
      oldValue={oldYaml}
      newValue={newYaml}
      compareMethod={DiffMethod.YAML}
      splitView={true}
    />
  );
}

Other diff methods

For regular code or text files, use the standard diff methods:

import React, { PureComponent } from 'react';
import ReactDiffViewer, { DiffMethod } from 'react-diff-viewer-continued';

const oldCode = `
const a = 10
const b = 10
`;
const newCode = `
const a = 10
const boo = 10
`;

class Diff extends PureComponent {
  render = () => {
    return (
      <ReactDiffViewer
        oldValue={oldCode}
        newValue={newCode}
        compareMethod={DiffMethod.WORDS}
        splitView={true}
      />
    );
  };
}

Overriding Styles

React Diff Viewer uses emotion for styling. It also offers a simple way to override styles and style variables. You can supply different variables for both light and dark themes. Styles will be common for both themes.

Below are the default style variables and style object keys.


// Default variables and style keys

const defaultStyles = {
  variables: {
    light: {
      diffViewerBackground: '#fff',
      diffViewerColor: '#212529',
      addedBackground: '#e6ffed',
      addedColor: '#24292e',
      removedBackground: '#ffeef0',
      removedColor: '#24292e',
      wordAddedBackground: '#acf2bd',
      wordRemovedBackground: '#fdb8c0',
      addedGutterBackground: '#cdffd8',
      removedGutterBackground: '#ffdce0',
      gutterBackground: '#f7f7f7',
      gutterBackgroundDark: '#f3f1f1',
      highlightBackground: '#fffbdd',
      highlightGutterBackground: '#fff5b1',
      codeFoldGutterBackground: '#dbedff',
      codeFoldBackground: '#f1f8ff',
      emptyLineBackground: '#fafbfc',
      gutterColor: '#212529',
      addedGutterColor: '#212529',
      removedGutterColor: '#212529',
      codeFoldContentColor: '#212529',
      diffViewerTitleBackground: '#fafbfc',
      diffViewerTitleColor: '#212529',
      diffViewerTitleBorderColor: '#eee',
    },
    dark: {
      diffViewerBackground: '#2e303c',
      diffViewerColor: '#FFF',
      addedBackground: '#044B53',
      addedColor: 'white',
      removedBackground: '#632F34',
      removedColor: 'white',
      wordAddedBackground: '#055d67',
      wordRemovedBackground: '#7d383f',
      addedGutterBackground: '#034148',
      removedGutterBackground: '#632b30',
      gutterBackground: '#2c2f3a',
      gutterBackgroundDark: '#262933',
      highlightBackground: '#2a3967',
      highlightGutterBackground: '#2d4077',
      codeFoldGutterBackground: '#21232b',
      codeFoldBackground: '#262831',
      emptyLineBackground: '#363946',
      gutterColor: '#464c67',
      addedGutterColor: '#8c8c8c',
      removedGutterColor: '#8c8c8c',
      codeFoldContentColor: '#555a7b',
      diffViewerTitleBackground: '#2f323e',
      diffViewerTitleColor: '#555a7b',
      diffViewerTitleBorderColor: '#353846',
    }
  },
  diffContainer?: {}, // style object
  diffRemoved?: {}, // style object
  diffAdded?: {}, // style object
  marker?: {}, // style object
  emptyGutter?: {}, // style object
  highlightedLine?: {}, // style object
  lineNumber?: {}, // style object
  highlightedGutter?: {}, // style object
  contentText?: {}, // style object
  gutter?: {}, // style object
  line?: {}, // style object
  wordDiff?: {}, // style object
  wordAdded?: {}, // style object
  wordRemoved?: {}, // style object
  codeFoldGutter?: {}, // style object
  codeFold?: {}, // style object
  emptyLine?: {}, // style object
  content?: {}, // style object
  titleBlock?: {}, // style object
  splitView?: {}, // style object
}

To override any style, just pass the new style object to the styles prop. New style will be computed using Object.assign(default, override).

For keys other than variables, the value can either be an object or string interpolation.

import React, { PureComponent } from 'react';
import ReactDiffViewer from 'react-diff-viewer-continued';

const oldCode = `
const a = 10
const b = 10
const c = () => console.log('foo')

if(a > 10) {
  console.log('bar')
}

console.log('done')
`;
const newCode = `
const a = 10
const boo = 10

if(a === 10) {
  console.log('bar')
}
`;

class Diff extends PureComponent {
  highlightSyntax = (str) => (
    <span
      style={{ display: 'inline' }}
      dangerouslySetInnerHTML={{
        __html: Prism.highlight(str, Prism.languages.javascript),
      }}
    />
  );

  render = () => {
    const newStyles = {
      variables: {
        dark: {
          highlightBackground: '#fefed5',
          highlightGutterBackground: '#ffcd3c',
        },
      },
      line: {
        padding: '10px 2px',
        '&:hover': {
          background: '#a26ea1',
        },
      },
    };

    return (
      <ReactDiffViewer
        styles={newStyles}
        oldValue={oldCode}
        newValue={newCode}
        splitView={true}
        renderContent={this.highlightSyntax}
      />
    );
  };
}

Local Development

pnpm install
pnpm build # or use yarn build:watch
pnpm start:examples

Check package.json for more build scripts.

Contributors

Eric M.
Eric M.

💻
Andrei Kovalevsky
Andrei Kovalevsky

💻
Chang Hyun Kim
Chang Hyun Kim

💻

License

MIT