Skip to content

Instantly share code, notes, and snippets.

@slavikshen
Created November 10, 2015 15:37
Show Gist options
  • Save slavikshen/7b29b06215b9e7a08455 to your computer and use it in GitHub Desktop.
Save slavikshen/7b29b06215b9e7a08455 to your computer and use it in GitHub Desktop.
How to get inner text in React virtual DOM
$(function() {
React.getInnerText = function(obj) {
var buf = '';
if( obj ) {
var type = typeof(obj);
if( type === 'string' || type === 'number' ) {
buf += obj;
} else if( type === 'object' ) {
var children = null;
if( Array.isArray(obj) ) {
children = obj;
} else {
var props = obj.props;
if( props ) {
children = props.children;
}
}
if( children ) {
if( Array.isArray(children) ) {
children.forEach(function(o) {
buf += React.getInnerText(o);
});
} else {
buf += React.getInnerText(children);
}
}
}
}
return buf;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment