Skip to content

Instantly share code, notes, and snippets.

@taylor-jones
Forked from alexeychikk/ReactComment.jsx
Last active February 19, 2019 19:22
Show Gist options
  • Save taylor-jones/2f3d8361fdc4ab5c565f4f17fd734df3 to your computer and use it in GitHub Desktop.
Save taylor-jones/2f3d8361fdc4ab5c565f4f17fd734df3 to your computer and use it in GitHub Desktop.
Simple React HTML comment
/* 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