-
-
Save taylor-jones/2f3d8361fdc4ab5c565f4f17fd734df3 to your computer and use it in GitHub Desktop.
Simple React HTML comment
This file contains hidden or 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
/* eslint-disable react/no-find-dom-node */ | |
import React, { PureComponent } from 'react'; | |
import ReactDOM from 'react-dom'; | |
import PropTypes from 'prop-types'; | |
class HtmlComment extends PureComponent { | |
componentDidMount() { | |
const node = ReactDOM.findDOMNode(this); | |
ReactDOM.unmountComponentAtNode(node); | |
node.outerHTML = this.createComment(); | |
} | |
createComment() { | |
const { text, trim } = this.props; | |
const comment = trim ? text.trim() : text; | |
return `<!-- ${comment} -->`; | |
} | |
render() { | |
return <div />; | |
} | |
} | |
HtmlComment.defaultProps = { | |
trim: true, | |
}; | |
HtmlComment.propTypes = { | |
text: PropTypes.string.isRequired, | |
trim: PropTypes.bool, | |
}; | |
export default HtmlComment; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment