Created
December 25, 2017 15:11
-
-
Save gilbarbara/71e2f1fb1afe56ff669fa2d69fc6f4d0 to your computer and use it in GitHub Desktop.
Convert DOM node to React Element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for your code.