Package Exports
- html-to-react
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (html-to-react) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
html-to-react
A lightweight library that converts raw HTML to a React DOM structure.
Why?
I had a scenario where an HTML template was generated by a different team, yet I wanted to leverage React for the parts I did have control over. The template basically contains something like:
<div class="row">
<div class="col-sm-6">
<div data-report-id="report-1">
<!-- A React component for report-1 -->
</div>
</div>
<div class="col-sm-6">
<div data-report-id="report-2">
<!-- A React component for report-2 -->
</div>
</div>
</div>
I had to replace each <div>
that contains a data-report-id
attribute with an actual report, which was nothing more
than a React component.
Simply replacing the <div>
elements with a React component would end up with multiple top-level React components
that have no common parent.
The html-to-react module solves this problem by parsing each DOM element and converting it to a React tree with one single parent.
Examples
Simple
The following example parses each node and its attributes and returns a React component.
var React = require('react');
var parser = new require('html-to-react').Parser(React);
var htmlInput = '<div><h1>Title</h1><p>A paragraph</p></div>';
var reactComponent = parser.parse(htmlInput);
var reactHtml = React.renderToStaticMarkup(reactComponent);
assert.equal(reactHtml, htmlInput);
Installation
$ npm install --save html-to-react