Skip to content

Instantly share code, notes, and snippets.

@alexeychikk
Last active September 5, 2022 17:35
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save alexeychikk/bfe72a072a9a962f2da900b6151e4aae to your computer and use it in GitHub Desktop.
Save alexeychikk/bfe72a072a9a962f2da900b6151e4aae to your computer and use it in GitHub Desktop.
Simple React HTML comment
/*
Usage (I however think that the code is self explanatory)
<ReactComment text={`
Very long comment with html link
<a href="https://gist.github.com/alexeychikk/bfe72a072a9a962f2da900b6151e4aae">Star me :)</a>
`} />
*/
import React, {Component, PropTypes} from 'react';
import ReactDOM from 'react-dom';
class ReactComment extends Component {
static propTypes = {
text: PropTypes.string,
trim: PropTypes.bool
};
static defaultProps = {
trim: true
};
componentDidMount() {
let el = ReactDOM.findDOMNode(this);
ReactDOM.unmountComponentAtNode(el);
el.outerHTML = this.createComment();
}
createComment() {
let text = this.props.text;
if (this.props.trim) {
text = text.trim();
}
return `<!-- ${text} -->`;
}
render() {
return <div />;
}
}
export default ReactComment;
@giwiro
Copy link

giwiro commented Jul 29, 2018

Great work pal !

@margaretbloom
Copy link

Meh :/
What's the point of dynamically generating comments?
Either the comments are for humans (and they'd be JSX comments) or for the browsers, like the IE conditional comments (and we need them to appear even before the html tag).

@ARXChrono
Copy link

1:1 warning React.PropTypes is deprecated since React 15.5.0, use the npm module prop-types instead react/no-deprecated
✖ 1 problem (0 errors, 1 warning)

@steven-r
Copy link

steven-r commented Mar 1, 2019

Meh :/
What's the point of dynamically generating comments?
Either the comments are for humans (and they'd be JSX comments) or for the browsers, like the IE conditional comments (and we need them to appear even before the html tag).

Some people hide stack traces inside comments to help support people to analyze those later on; and yes, they might be smarter options, but some (esp. large) projects got used to it

@nickcooley
Copy link

Meh :/
What's the point of dynamically generating comments?
Either the comments are for humans (and they'd be JSX comments) or for the browsers, like the IE conditional comments (and we need them to appear even before the html tag).

Not ALL IE conditional tags happen before the <html>-- <picture> for example, has an IE hack as well...

<picture >
 <!--[if IE 9]><video style="display: none"><![endif]--></video>

@robwelan
Copy link

Meh :/
What's the point of dynamically generating comments?
Either the comments are for humans (and they'd be JSX comments) or for the browsers, like the IE conditional comments (and we need them to appear even before the html tag).

Look up how InfoLinks asks you to help them determine where to insert Ads.

@FibreFoX
Copy link

Just as a note, this does not work when using renderToStaticMarkup of react-dom/server, because componentDidMount does not get triggered. For reference: facebook/react#16931

For me generating comments is required, not only "inside the browser" but when using react as some part of static-file-generation outside of the browser.

@alexeychikk
Copy link
Author

Just as a note, this does not work when using renderToStaticMarkup of react-dom/server, because componentDidMount does not get triggered. For reference: facebook/react#16931

For me generating comments is required, not only "inside the browser" but when using react as some part of static-file-generation outside of the browser.

Try this https://stackoverflow.com/a/56136178/4373305

@FibreFoX
Copy link

FibreFoX commented Oct 2, 2019

@alexeychikk That won't work, as it always creates a wrapper-element

@certainlyakey
Copy link

This causes a crash for me when navigating with react-router-dom to a component where ReactComment is not used.

@Zneider
Copy link

Zneider commented Feb 18, 2020

I have made an npm package for this: https://www.npmjs.com/package/react-html-comment

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