Created
September 27, 2016 14:43
-
-
Save xaviervia/9a4f4aac4f6fb326e118453527dfa222 to your computer and use it in GitHub Desktop.
How to add Comment Nodes in React
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
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