Convert HTML/XML source code or DOM nodes to React elements
Installations
npm install react-from-dom
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Node Version
20.11.1
NPM Version
10.2.4
Score
95.5
Supply Chain
98.7
Quality
79
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
gilbarbara
Download Statistics
Total Downloads
43,295,662
Last Day
17,178
Last Week
133,646
Last Month
1,013,661
Last Year
13,172,663
GitHub Statistics
28 Stars
66 Commits
4 Forks
3 Watching
1 Branches
2 Contributors
Bundle Size
12.25 kB
Minified
4.58 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.7.3
Package Id
react-from-dom@0.7.3
Unpacked Size
128.26 kB
Size
24.43 kB
File Count
11
NPM Version
10.2.4
Node Version
20.11.1
Publised On
07 Mar 2024
Total Downloads
Cumulative downloads
Total Downloads
43,295,662
Last day
-63.2%
17,178
Compared to previous day
Last week
-48.5%
133,646
Compared to previous week
Last month
-14.4%
1,013,661
Compared to previous month
Last year
2.5%
13,172,663
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
Dev Dependencies
26
react-from-dom
Convert HTML/XML source code or a DOM node to a React element.
The perfect replacement for React's dangerouslySetInnerHTML
Setup
Install it
1npm install react-from-dom
Getting Started
Set a string with HTML/XML source code OR a DOM Node, which will be used to create React elements recursively.
1import React from 'react'; 2import convert from 'react-from-dom'; 3 4const panel = convert(` 5<div class="panel"> 6 <div class="panel-header"> 7 <h2>Title</h2> 8 </div> 9 <div class="panel-content"> 10 <ul> 11 <li>line 1</li> 12 <li>line 2</li> 13 </ul> 14 </div> 15 <div class="panel-footer"> 16 Footer 17 </div> 18</div> 19`); 20 21const audio = document.createElement('audio'); 22audio.setAttribute('controls', 'true'); 23audio.setAttribute( 24 'src', 25 'https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3', 26); 27const audioContent = document.createTextNode('Your browser does not support the audio element.'); 28audio.appendChild(audioContent); 29 30const audioElement = convert(audio); 31 32const App = () => ( 33 <div> 34 {panel} 35 {audioElement} 36 </div> 37);
API
The function accepts two parameters:
input string|Node
- required
An HTML/XML source code string or a DOM node.
options Options
- actions
Action[]
An array of actions to modify the nodes before converting them to ReactNodes.
Read about them below. - allowWhiteSpaces
boolean
▶︎ false
Don't remove white spaces in the output. - includeAllNodes
boolean
▶︎ false
Parse all nodes instead of just a single parent node.
This will return a ReactNode array (or a NodeList ifnodeOnly
is true) - Index
number
▶︎ 0
The index to start the React key identification. - level
number
▶︎ 0
The level to start the React key identification. - nodeOnly
boolean
▶︎ false
Return the node (or NodeList) without converting it to a ReactNode.
Only used for string inputs. - randomKey
boolean
▶︎ false
Add a random key to the root element. - selector
string
▶︎ **body > ***
The selector to use in thedocument.querySelector
method.
Only used for string inputs. - type
DOMParserSupportedType
▶︎ text/html
The mimeType to use in the DOMParser's parseFromString.
Only used for string inputs.
Actions
You can mutate/update a Node before the conversion or replace it with a ReactNode.
1{ 2 // If this returns true, the two following functions are called if they are defined 3 condition: (node: Node, key: string, level: number) => boolean; 4 5 // Use this to update or replace the node 6 // e.g. for removing or adding attributes, changing the node type 7 pre?: (node: Node, key: string, level: number) => Node; 8 9 // Use this to inject a component or remove the node 10 // It must return something that can be rendered by React 11 post?: (node: Node, key: string, level: number) => React.ReactNode; 12}
Examples
Add a class to all elements that match.
1{ 2 condition: node => node.nodeName.toLowerCase() === 'div', 3 pre: node => { 4 node.className += ' a-class-added'; 5 return node; 6 }, 7}
Remove all elements with a specific class.
1{ 2 condition: node => node.className.indexOf('delete-me') >= 0, 3 post: () => null, 4}
Return a react component for some node types.
1{ 2 condition: node => node.nodeName.toLowerCase() === 'pre', 3 post: (node, key) => ( 4 <ReactMarkdown key={key} source={node.textContent} /> 5 ), 6},
Transform one node into another and preserve the child nodes.
1{ 2 condition: node => node.nodeName.toLowerCase() === 'ul', 3 pre: (node) => { 4 const ol = document.createElement('ol'); 5 6 [...node.childNodes].forEach(child => { 7 ol.appendChild(child); 8 }); 9 10 return ol; 11 } 12}
Browser Support
If you need to support legacy browsers, you'll need to include a polyfiil for Number.isNaN
in your app.
Take a look at react-app-polyfill or polyfill.io.
Credits
This is a fork from the dom-to-react package. Thanks! ❤️
License
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/main.yml:15
Reason
6 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/21 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/gilbarbara/react-from-dom/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/gilbarbara/react-from-dom/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/gilbarbara/react-from-dom/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:38: update your workflow using https://app.stepsecurity.io/secureworkflow/gilbarbara/react-from-dom/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:49: update your workflow using https://app.stepsecurity.io/secureworkflow/gilbarbara/react-from-dom/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:67: update your workflow using https://app.stepsecurity.io/secureworkflow/gilbarbara/react-from-dom/main.yml/main?enable=pin
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 3 third-party GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 14 are checked with a SAST tool
Score
3.6
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-from-dom
@eslint-react/eslint-plugin
The main ESLint plugin that contains all the rules from eslint-plugin-react-[x, dom, web-api, hooks-extra, naming-convention].
react-router-dom-v5-compat
Migration path to React Router v6 from v4/5
react-dom-instance
Get react component instances from a DOM node
inferno-dom
Provides methods to render DOM nodes from Inferno elements