React XML Viewer
react-xml-viewer is a React component designed for pretty-printing and displaying XML strings in a user-friendly, formatted manner. The current stable version is 3.0.4. The library has an active release cadence, with recent updates addressing security vulnerabilities (CVEs), ensuring compatibility with newer React versions (e.g., React 19), and adding new features like line numbers and improved collapsing functionality. Its key differentiators include extensive customization options for appearance via a theming prop, the ability to make XML collapsible, and a dedicated prop for handling invalid XML input. It also ships with TypeScript types, enhancing developer experience in TypeScript projects. It leverages `fast-xml-parser` for its underlying XML processing, and regular updates help maintain security and performance.
Common errors
-
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
cause Attempting to use `require('react-xml-viewer')` directly in a CommonJS module when `react-xml-viewer` is primarily an ESM default export.fixUse `const XMLViewer = require('react-xml-viewer').default;` for CommonJS imports or switch to `import XMLViewer from 'react-xml-viewer';` in an ESM context. -
TypeError: Cannot read properties of undefined (reading 'split')
cause Passing a non-string or `null`/`undefined` value to the `xml` prop, which expects a string.fixEnsure the `xml` prop always receives a valid XML string. For optional XML, use conditional rendering or provide a default empty string: `<XMLViewer xml={xmlString ?? ''} />`. -
Property 'initalCollapsedDepth' does not exist on type 'IntrinsicAttributes & XMLViewerProps'. Did you mean 'initialCollapsedDepth'?
cause Using the old, misspelled prop `initalCollapsedDepth` in TypeScript projects.fixUpdate `react-xml-viewer` to version 2.0.4 or higher and correct the prop name to `initialCollapsedDepth`.
Warnings
- breaking Version 2.0.0 introduced significant changes, potentially breaking existing implementations built for v1.x. Users upgrading should review the changelog for specific migration steps.
- breaking The `initialCollapsedDepth` prop had a typo as `initalCollapsedDepth` in versions prior to 2.0.4. Using the misspelled prop would have no effect.
- gotcha The component requires valid XML. If the `xml` prop contains malformed XML, it will display the content provided by the `invalidXml` prop (defaulting to 'Invalid XML!'). This can lead to unexpected UI if not handled.
- breaking A series of security vulnerabilities (CVE-2026-25896, CVE-2026-26278) related to the underlying `fast-xml-parser` dependency were patched in version 3.0.2.
- gotcha While v2.0.5 added compatibility for React 19, earlier versions might not work correctly with React 19. Similarly, v2.0.0 included a fix for React 16 compatibility.
Install
-
npm install react-xml-viewer -
yarn add react-xml-viewer -
pnpm add react-xml-viewer
Imports
- XMLViewer
import { XMLViewer } from 'react-xml-viewer';import XMLViewer from 'react-xml-viewer';
- XMLViewer (CommonJS)
const XMLViewer = require('react-xml-viewer');const XMLViewer = require('react-xml-viewer').default; - Theme customization
const customTheme = { tagColor: '#d43900' }; // ... <XMLViewer theme={customTheme} />
Quickstart
import React from 'react';
import XMLViewer from 'react-xml-viewer';
const exampleXml = `
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J.K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
`;
const customInvalidXmlMessage = (
<div style={{ color: 'red', fontWeight: 'bold' }}>
Oops! This XML is malformed.
</div>
);
export function App() {
return (
<div style={{ fontFamily: 'monospace', padding: '20px' }}>
<h1>My XML Document</h1>
<XMLViewer
xml={exampleXml}
indentSize={4}
collapsible={true}
initialCollapsedDepth={1}
showLineNumbers={true}
invalidXml={customInvalidXmlMessage}
theme={{
tagColor: '#61dafb',
textColor: '#212121',
attributeKeyColor: '#e91e63',
attributeValueColor: '#4caf50'
}}
/>
</div>
);
}