Skip to content

Instantly share code, notes, and snippets.

@xaviervia
Created September 27, 2016 14:43
Show Gist options
  • Save xaviervia/9a4f4aac4f6fb326e118453527dfa222 to your computer and use it in GitHub Desktop.
Save xaviervia/9a4f4aac4f6fb326e118453527dfa222 to your computer and use it in GitHub Desktop.
How to add Comment Nodes in React
import React from 'react'
const NODE_COMMENT = 8
export default React.createClass({
displayName: 'CommentNodeInSpan',
render () {
const {comment} = this.props
return (
<span ref={(span) => {
if (span == null) {
return // Because the callback is called even when unmounting
}
if (span.childNodes[0].nodeType === NODE_COMMENT && span.childNodes[0].textContent === comment) {
return // The comment was already here
}
span.appendChild(document.createComment(comment))
}} />
)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment