Skip to content

Instantly share code, notes, and snippets.

@gilbarbara
Created December 25, 2017 15:11
Show Gist options
  • Select an option

  • Save gilbarbara/71e2f1fb1afe56ff669fa2d69fc6f4d0 to your computer and use it in GitHub Desktop.

Select an option

Save gilbarbara/71e2f1fb1afe56ff669fa2d69fc6f4d0 to your computer and use it in GitHub Desktop.
Convert DOM node to React Element
@@ -0,0 +1,21 @@
import React from 'react';
import PropTypes from 'prop-types';
import ReactHtmlParser from 'react-html-parser';
export default class HtmlWrapper extends React.Component {
static propTypes = {
element: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
]).isRequired,
};
render() {
if (!this.props.element) return null;
const { element } = this.props;
const node = typeof element === 'string' ? document.body.querySelector(element) : element;
return ReactHtmlParser(node.outerHTML);
}
}
@quyle92
Copy link
Copy Markdown

quyle92 commented Jun 24, 2022

thanks for your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment