React innerText Extractor
react-innertext is a utility package designed to extract the plain text content from React JSX objects, mimicking the behavior of the native DOM `innerText` property. It recursively traverses React elements, including children and fragments, to produce a single, concatenated string. This is particularly useful for scenarios requiring a plain text representation of complex React component trees, such as generating `title` attributes, SEO metadata, or for accessibility purposes where only the text content is needed. The current stable version is 1.1.5. While its release cadence appears infrequent based on the provided history, the project is maintained, with recent updates (v1.1.4) addressing Node.js compatibility and incorporating built-in TypeScript definitions. Its key differentiator is its straightforward API for handling React's virtual DOM, providing a direct equivalent to `innerText` without requiring a full DOM render or complex parsing logic.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'children') or similar errors related to 'window' object
cause Using `react-innertext` in a Node.js (server-side rendering) environment with a version older than 1.1.4, which had dependencies on browser-specific globals.fixUpdate `react-innertext` to version 1.1.4 or newer: `npm install react-innertext@latest` or `yarn upgrade react-innertext@latest`. -
TypeError: innerText is not a function or TypeError: (0 , react_innertext__WEBPACK_IMPORTED_MODULE_2__.innerText) is not a function
cause Incorrect import statement for `react-innertext` in an ESM project, trying to use a named import when it provides a default export.fixChange `import { innerText } from 'react-innertext';` to `import innerText from 'react-innertext';`.
Warnings
- gotcha This utility extracts text from React elements and does not simulate full browser rendering. It will not execute JavaScript within the JSX or account for CSS styling that might visually hide or modify text content in a browser environment.
- breaking Prior to version 1.1.4, `react-innertext` had issues running in `window`-less environments, such as Node.js for server-side rendering (SSR). This could lead to errors or incorrect behavior during SSR.
- gotcha When migrating existing CommonJS projects to use ESM, ensure you update your import statements from `require()` to `import` statements and correctly handle the default export.
Install
-
npm install react-innertext -
yarn add react-innertext -
pnpm add react-innertext
Imports
- innerText
import { innerText } from 'react-innertext';import innerText from 'react-innertext';
- innerText
const innerText = require('react-innertext'); - innerText
import innerText from 'react-innertext';
Quickstart
import innerText from 'react-innertext';
import React from 'react';
function MyComponent() {
const children = (
<div>
Hello <strong>world</strong>!
I am <span children={3} /> years old!
</div>
);
const extractedText = innerText(children);
console.log(extractedText); // Expected: 'Hello world! I am 3 years old!'
// A practical example: using innerText for a title attribute
function MyTableHeaderCell({ children }) {
return (
<th
children={children}
title={innerText(children)}
/>
);
}
return (
<div>
<p>Original JSX:</p>
{children}
<p>Extracted text: {extractedText}</p>
<table>
<thead>
<tr>
<MyTableHeaderCell>
<b>Username</b>
</MyTableHeaderCell>
</tr>
</thead>
</table>
</div>
);
}
// In a real application, you would render MyComponent
// ReactDOM.render(<MyComponent />, document.getElementById('root'));